135 lines
3.3 KiB
C#
135 lines
3.3 KiB
C#
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace asg_form.Controllers;
|
|
|
|
public class T_config
|
|
{
|
|
public int Id { set; get; }
|
|
public string Title{ set; get; }
|
|
public string Substance{ set; get;}
|
|
public string? msg{ set; get; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class config : ControllerBase
|
|
{
|
|
[Route("api/v1/admin/config")]
|
|
[HttpPost]
|
|
[Authorize]
|
|
public async Task<ActionResult<object>> config_post([FromBody] T_config config)
|
|
{
|
|
|
|
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
|
|
{
|
|
return BadRequest(new error_mb { code = 400, message = "无权访问" });
|
|
}
|
|
using(TestDbContext db=new TestDbContext()){
|
|
var config__ = db.T_config.FirstOrDefault(a => a.Id == config.Id);
|
|
if (config__==null)
|
|
{
|
|
db.T_config.Add(config);
|
|
await db.SaveChangesAsync();
|
|
}
|
|
else
|
|
{
|
|
var config_ = db.T_config.FirstOrDefault(a => a.Id == config.Id);
|
|
|
|
config_.msg=config.msg;
|
|
config_.Substance = config.Substance;
|
|
config_.Title = config.Title;
|
|
await db.SaveChangesAsync();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
return Ok("添加成功!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Route("api/v1/admin/config/byTitle")]
|
|
[HttpPost]
|
|
[Authorize]
|
|
public async Task<ActionResult<object>> config_get_title([FromBody] string title)
|
|
{
|
|
|
|
|
|
|
|
using (TestDbContext db = new TestDbContext())
|
|
{
|
|
var config = db.T_config.FirstOrDefault(a => a.Title == title);
|
|
return Ok(config.Substance);
|
|
}
|
|
|
|
}
|
|
|
|
[Route("api/v1/admin/config")]
|
|
[HttpDelete]
|
|
[Authorize]
|
|
public async Task<ActionResult<object>> config_get_title(int Id)
|
|
{
|
|
|
|
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
|
|
{
|
|
return BadRequest(new error_mb { code = 400, message = "无权访问" });
|
|
}
|
|
using (TestDbContext db = new TestDbContext())
|
|
{
|
|
var config = db.T_config.FirstOrDefault(a => a.Id==Id);
|
|
db.Remove(config);
|
|
await db.SaveChangesAsync();
|
|
return Ok("成功!");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Route("api/v1/admin/config/all")]
|
|
[HttpGet]
|
|
public async Task<ActionResult<object>> config_get_all(string msg="null",short page=1, short limit = 10)
|
|
{
|
|
|
|
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
|
|
{
|
|
return BadRequest(new error_mb { code = 400, message = "无权访问" });
|
|
}
|
|
using (TestDbContext db = new TestDbContext())
|
|
{
|
|
var query = db.T_config.AsQueryable().Where(n => msg=="null" || n.msg==null || n.msg.Contains(msg) || n.Title.Contains(msg) || n.Substance.Contains(msg));
|
|
var TotalRecords = await query.CountAsync();
|
|
var config =await query
|
|
.Skip((page - 1) * limit)
|
|
.Take(limit)
|
|
.ToListAsync();
|
|
var data = new
|
|
{
|
|
rows = config,
|
|
total = TotalRecords,
|
|
};
|
|
return Ok(new { code = 200, message = "", data });
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|