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 { [ApiController] public class Storehttp : ControllerBase { private readonly RoleManager roleManager; private readonly UserManager userManager; public Storehttp( RoleManager roleManager, UserManager userManager) { this.roleManager = roleManager; this.userManager = userManager; } [Route("api/v1/admin/Store")] [HttpPost] [Authorize] public async Task> 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> 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> 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); a.Name=storeinfo.Name; a.description=storeinfo.description; a.information=storeinfo.information; a.Price=storeinfo.Price; await sb.SaveChangesAsync(); return Ok(storeinfo); } } public static long cut_value(long value,long money) { long _value = value; value = value - money; if (value < 0) { throw new ArgumentException("你已经没钱啦!"); } return value; } [Route("api/v1/Store")] [HttpGet] [Authorize] public async Task> GetStore() { using (TestDbContext sb = new TestDbContext()) { var a= sb.T_Store.ToList(); return Ok(a); } } [Route("api/v1/Store/Verification")] [HttpGet] [Authorize] public async Task> 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(); return Ok(a); } } [Route("api/v1/Store/my")] [HttpGet] [Authorize] public async Task> mybuy() { long id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value.ToInt64(); using (TestDbContext sb = new TestDbContext()) { 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(); return Ok(a); } } /// /// /// /// /// /// /// 是否展示以及核销过的 /// [Route("api/v1/admin/Storeinfo")] [HttpGet] [Authorize] public async Task> 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()) { var a = new all_record(); IQueryable b; if (showVerification) { b = sb.T_Storeinfo.Include(a=>a.Store); } else { b = sb.T_Storeinfo.Include(a => a.Store).Where(a => a.isVerification == false); } if (search_id == null) { a.cout = b.Count(); 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(); } else { a.cout = b.Where(a => a.buyerid == search_id).Count(); 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(); } return Ok(a); } } public record buyreq_record(bool iserror, string msg); public record all_record() { public long? cout { get; set; } public object msg { get; set; } } [Route("api/v1/Store/Buy")] [HttpPost] [Authorize] public async Task> BuyStore([FromBody]long[] storeid) { 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()) { List bureq = new List(); foreach (var item in storeid) { var stort = await sb.T_Store.FindAsync(item); 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, $"购买失败,因为余额不足")); } } return Ok(bureq); } } } }