134 lines
3.3 KiB
C#
Raw Permalink Normal View History

2024-08-11 00:16:14 +08:00
using System.Security.Claims;
2024-10-07 11:05:03 +08:00
using System.Threading.Tasks;
2024-08-11 00:16:14 +08:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2024-10-07 11:05:03 +08:00
using Microsoft.EntityFrameworkCore;
2024-08-11 00:16:14 +08:00
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; }
}
2025-02-09 19:17:34 +08:00
2024-08-11 00:16:14 +08:00
[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]
public async Task<ActionResult<object>> config_get_title([FromBody] string title)
{
2024-08-11 17:29:12 +08:00
2024-08-11 00:16:14 +08:00
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]
2024-11-07 14:33:50 +08:00
public async Task<ActionResult<object>> config_get_all(string msg="null",short page=1, short limit = 10)
2024-08-11 00:16:14 +08:00
{
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
return BadRequest(new error_mb { code = 400, message = "无权访问" });
}
using (TestDbContext db = new TestDbContext())
{
2024-11-07 16:31:07 +08:00
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));
2024-10-07 11:05:03 +08:00
var TotalRecords = await query.CountAsync();
var config =await query
.Skip((page - 1) * limit)
.Take(limit)
.ToListAsync();
2024-11-07 16:31:07 +08:00
var data = new
2024-08-11 00:16:14 +08:00
{
2024-10-07 11:05:03 +08:00
rows = config,
total = TotalRecords,
};
2024-11-07 16:31:07 +08:00
return Ok(new { code = 200, message = "", data });
2024-08-11 00:16:14 +08:00
}
}
}