140 lines
4.4 KiB
C#
Raw Normal View History

2024-08-11 00:16:14 +08:00
using Manganese.Array;
2024-10-04 16:16:48 +08:00
using Microsoft.AspNetCore.Authorization;
2024-08-03 20:40:34 +08:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;
namespace asg_form.Controllers
{
public class Champion : ControllerBase
{
private readonly RoleManager<Role> roleManager;
private readonly UserManager<User> userManager;
public Champion(
RoleManager<Role> roleManager, UserManager<User> userManager)
{
this.roleManager = roleManager;
this.userManager = userManager;
}
/// <summary>
/// 修改冠军
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
[Route("api/v1/admin/Champion/")]
[HttpPut]
public async Task<ActionResult<string>> Putchampion([FromBody] req_Champion req)
{
if (this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
TestDbContext testDb = new TestDbContext();
var form = testDb.Forms.First(x => x.Id == req.formId);
var events = testDb.events.First(x => x.name == req.eventname);
var chap = testDb.Champions.First(x => x.form == form);
chap.events = events;
chap.msg = req.msg;
await testDb.SaveChangesAsync();
return Ok();
}
return BadRequest(new error_mb { code = 400, message = "你没有管理员呢~" });
}
2024-10-04 16:16:48 +08:00
[Route("api/v1/admin/Champion/")]
2024-08-03 20:40:34 +08:00
[HttpPost]
2024-10-04 16:16:48 +08:00
[Authorize]
2024-08-03 20:40:34 +08:00
public async Task<ActionResult<string>> Postchampion([FromBody]req_Champion req)
{
if (this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
2024-10-04 16:23:24 +08:00
2024-10-04 16:16:48 +08:00
int err = 0;
try
{
2024-10-04 16:23:24 +08:00
TestDbContext testDb = new TestDbContext();
2024-10-04 16:16:48 +08:00
var form = testDb.Forms.First(x => x.Id == req.formId);
err = 1;
var events = testDb.events.First(x => x.name == req.eventname);
err = 2;
testDb.Champions.Add(new T_Champion { events = events, form = form ,msg=req.msg});
err = 3;
2024-10-04 16:23:24 +08:00
await testDb.SaveChangesAsync();
return Ok();
2024-10-04 16:16:48 +08:00
}
catch (Exception ex)
{
return Ok(new { code = 500, message = "服务器错误",detailcode=err, details = ex.Message });
}
2024-10-04 16:23:24 +08:00
2024-08-03 20:40:34 +08:00
}
return BadRequest(new error_mb { code = 400, message = "你没有管理员呢~" });
}
/// <summary>
/// 删除冠军
/// </summary>
/// <returns></returns>
[Route("api/v1/admin/Champion/")]
[HttpDelete]
public async Task<ActionResult<List<Champion.T_Champion>>> delchampion(int id )
{
if (this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
TestDbContext testDb = new TestDbContext();
var chaps = testDb.Champions.First(a=>a.Id==id);
testDb.Champions.Remove(chaps);
await testDb.SaveChangesAsync();
return Ok();
}
return BadRequest(new error_mb { code = 400, message = "你没有管理员呢~" });
}
2024-08-19 15:48:17 +08:00
[Route("api/v1/Champion/")]
2024-08-03 20:40:34 +08:00
[HttpGet]
public async Task<ActionResult<List<Champion.T_Champion>>> getchampion()
{
TestDbContext testDbContext = new TestDbContext();
var chaps = testDbContext.Champions.Include(a => a.form.role).Include(a => a.events).ToList();
foreach (var chap in chaps)
{
foreach (var item in chap.form.role)
{
item.form = null;
}
chap.events.forms = null;
}
return chaps;
}
public class T_Champion
{
public long Id { get; set; }
public form form { get; set; }
2024-08-25 00:21:41 +08:00
public T_events events { get; set; }
2024-08-03 20:40:34 +08:00
public string msg { get; set; }
}
public class req_Champion
{
public long formId { get; set; }
public string eventname { get; set; }
public string msg { get; set; }
}
}
}