234 lines
7.9 KiB
C#
Raw Normal View History

2024-08-10 19:01:49 +08:00
using Masuit.Tools;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;
namespace asg_form.Controllers.Store
{
2025-01-27 20:32:23 +08:00
2025-02-09 19:17:34 +08:00
2024-08-10 19:01:49 +08:00
public class Storehttp : ControllerBase
{
private readonly RoleManager<Role> roleManager;
private readonly UserManager<User> userManager;
public Storehttp(
RoleManager<Role> roleManager, UserManager<User> userManager)
{
this.roleManager = roleManager;
this.userManager = userManager;
}
[Route("api/v1/admin/Store")]
[HttpPost]
[Authorize]
public async Task<ActionResult<object>> AddStore([FromBody]StoreDB storeinfo)
{
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
return BadRequest(new error_mb { code = 400, message = "无权访问" });
}
using (TestDbContext sb = new TestDbContext()) {
sb.T_Store.Add(storeinfo);
await sb.SaveChangesAsync();
return Ok(storeinfo);
}
}
[Route("api/v1/admin/Store")]
[HttpDelete]
[Authorize]
public async Task<ActionResult<object>> DelStore(long id)
{
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
return BadRequest(new error_mb { code = 400, message = "无权访问" });
}
using (TestDbContext sb = new TestDbContext())
{
sb.T_Store.Remove(sb.T_Store.Find(id));
await sb.SaveChangesAsync();
return Ok("ok");
}
}
[Route("api/v1/admin/Store")]
[HttpPut]
[Authorize]
public async Task<ActionResult<object>> putStore([FromBody] StoreDB storeinfo)
{
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
return BadRequest(new error_mb { code = 400, message = "无权访问" });
}
using (TestDbContext sb = new TestDbContext())
{
var a= await sb.T_Store.FindAsync(storeinfo.id);
2024-08-11 00:16:14 +08:00
a.Name=storeinfo.Name;
a.description=storeinfo.description;
a.information=storeinfo.information;
a.Price=storeinfo.Price;
2024-08-10 19:01:49 +08:00
await sb.SaveChangesAsync();
return Ok(storeinfo);
}
}
2025-01-27 20:32:23 +08:00
public static long cut_value(long value,long money)
2024-08-10 19:01:49 +08:00
{
long _value = value;
value = value - money;
if (value < 0)
{
throw new ArgumentException("你已经没钱啦!");
}
return value;
}
[Route("api/v1/Store")]
[HttpGet]
[Authorize]
public async Task<ActionResult<object>> GetStore()
{
using (TestDbContext sb = new TestDbContext())
{
2024-08-11 00:16:14 +08:00
var a= sb.T_Store.ToList();
return Ok(a);
}
}
[Route("api/v1/Store/Verification")]
[HttpGet]
[Authorize]
public async Task<ActionResult<object>> Verification(long storeinfoid)
{
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
return BadRequest(new error_mb { code = 400, message = "无权访问" });
}
using (TestDbContext sb = new TestDbContext())
{
var a = sb.T_Storeinfo.Find(storeinfoid);
a.isVerification = true;
await sb.SaveChangesAsync();
2024-08-10 19:01:49 +08:00
return Ok(a);
}
}
2024-08-11 17:29:12 +08:00
2024-08-17 17:37:47 +08:00
[Route("api/v1/Store/my")]
[HttpGet]
[Authorize]
public async Task<ActionResult<object>> mybuy()
{
long id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value.ToInt64();
using (TestDbContext sb = new TestDbContext())
{
2024-08-17 23:08:15 +08:00
var a = await sb.T_Storeinfo.Include(a => a.Store).Select(a => new {a.id,a.buyerid,a.isVerification,a.Store.Name,a.Store.information,a.Store.description}).Where(a=>a.buyerid==id).ToListAsync();
2024-08-17 17:37:47 +08:00
return Ok(a);
}
}
2024-08-11 17:29:12 +08:00
2024-08-10 19:01:49 +08:00
/// <summary>
///
/// </summary>
/// <param name="search"></param>
/// <param name="pageindex"></param>
/// <param name="pagesize"></param>
/// <param name="showVerification">是否展示以及核销过的</param>
/// <returns></returns>
[Route("api/v1/admin/Storeinfo")]
[HttpGet]
[Authorize]
public async Task<ActionResult<object>> GetStoreinfo(bool showVerification,long? search_id,int pageindex=0,int pagesize=10)
{
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
return BadRequest(new error_mb { code = 400, message = "无权访问" });
}
using (TestDbContext sb = new TestDbContext())
{
2024-08-11 00:16:14 +08:00
var a = new all_record();
2024-08-10 19:01:49 +08:00
IQueryable<StoreinfoDB> b;
if (showVerification)
{
2024-08-11 17:29:12 +08:00
b = sb.T_Storeinfo.Include(a=>a.Store);
2024-08-10 19:01:49 +08:00
}
else
{
2024-08-11 17:29:12 +08:00
b = sb.T_Storeinfo.Include(a => a.Store).Where(a => a.isVerification == false);
2024-08-10 19:01:49 +08:00
}
if (search_id == null)
{
2024-08-11 00:16:14 +08:00
a.cout = b.Count();
2024-08-11 17:29:12 +08:00
a.msg = await b.Paginate(pageindex, pagesize).Select(a => new { a.id, a.buyerid, a.Store.Price, a.Store.description, a.isVerification, a.Store.information, a.Store.Name }).ToListAsync();
2024-08-11 00:16:14 +08:00
2024-08-10 19:01:49 +08:00
}
else
{
2024-08-11 00:16:14 +08:00
a.cout = b.Where(a => a.buyerid == search_id).Count();
2024-08-11 17:29:12 +08:00
a.msg = await b.Where(a => a.buyerid == search_id).Paginate(pageindex, pagesize).Select(a => new { a.id, a.buyerid, a.Store.Price, a.Store.description, a.isVerification, a.Store.information, a.Store.Name }).ToListAsync();
2024-08-10 19:01:49 +08:00
}
2024-08-11 17:29:12 +08:00
2024-08-11 00:16:14 +08:00
return Ok(a);
2024-08-10 19:01:49 +08:00
}
}
2024-08-11 00:16:14 +08:00
public record buyreq_record(bool iserror, string msg);
public record all_record()
{
public long? cout { get; set; }
public object msg { get; set; }
}
2024-08-10 19:01:49 +08:00
[Route("api/v1/Store/Buy")]
2024-08-11 00:16:14 +08:00
[HttpPost]
2024-08-10 19:01:49 +08:00
[Authorize]
2024-08-11 00:16:14 +08:00
public async Task<ActionResult<object>> BuyStore([FromBody]long[] storeid)
2024-08-10 19:01:49 +08:00
{
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
var user = await userManager.FindByIdAsync(id);
if (user.officium != "Commentator")
{
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });
}
using (TestDbContext sb = new TestDbContext())
{
2024-08-11 00:16:14 +08:00
List<buyreq_record> bureq = new List<buyreq_record>();
foreach (var item in storeid)
2024-08-10 19:01:49 +08:00
{
2024-08-11 17:29:12 +08:00
var stort = await sb.T_Store.FindAsync(item);
2024-08-11 00:16:14 +08:00
try
{
user.Integral = cut_value((long)user.Integral, stort.Price);
await userManager.UpdateAsync(user);
await sb.T_Storeinfo.AddAsync(new StoreinfoDB { buyerid = id.ToInt64(), Store = stort });
await sb.SaveChangesAsync();
bureq.Add(new buyreq_record(false, $"购买{stort.Name}成功"));
}
catch
{
bureq.Add(new buyreq_record(true, $"购买失败,因为余额不足"));
2024-08-10 19:01:49 +08:00
2024-08-11 00:16:14 +08:00
}
2024-08-10 19:01:49 +08:00
}
2024-08-11 00:16:14 +08:00
return Ok(bureq);
2024-08-10 19:01:49 +08:00
}
}
}
}