更新了解说兑换商店

This commit is contained in:
luolangaga 2024-08-10 19:01:49 +08:00
parent 0476e13241
commit 26b6308776
7 changed files with 286 additions and 34 deletions

View File

@ -1,4 +1,5 @@
using asg_form.Migrations; using asg_form.Migrations;
using Manganese.Text;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -36,7 +37,7 @@ namespace asg_form.Controllers
{ {
TestDbContext testDb = new TestDbContext(); TestDbContext testDb = new TestDbContext();
string chinaname = user.chinaname; string chinaname = user.chinaname;
var teamgame = testDb.team_Games.Where(a => a.commentary.IndexOf(chinaname) >= 0).Select(a => new { a.id, a.team1_name, a.team2_name, a.bilibiliuri, a.commentary, a.referee ,a.opentime}).ToList(); var teamgame = testDb.team_Games.Where(a => a.commentary.IndexOf(chinaname) >= 0).Select(a => new { a.id, a.team1_name, a.team2_name, a.bilibiliuri, a.commentary, a.referee, a.opentime,a.tag,a.belong}).ToList();
return JsonConvert.SerializeObject(teamgame); return JsonConvert.SerializeObject(teamgame);
} }
@ -55,25 +56,16 @@ namespace asg_form.Controllers
var user = await userManager.FindByIdAsync(id); var user = await userManager.FindByIdAsync(id);
if (user.officium == "Commentator") if (user.officium == "Commentator")
{ {
TestDbContext testDb = new TestDbContext(); TestDbContext testDb = new TestDbContext();
string chinaname = user.chinaname; string chinaname = user.chinaname;
var teamgame = await testDb.team_Games.FirstAsync(a => a.id == gameid); var teamgame = await testDb.team_Games.FirstAsync(a => a.id == gameid);
if (teamgame.commentary == "待公布") var com = JsonConvert.DeserializeObject<List<com_json>>(teamgame.commentary);
{ com.Add(new com_json { id = id.ToInt32(), chinaname = chinaname });
List<string> strings = new List<string> teamgame.commentary = JsonConvert.SerializeObject(com);
{
chinaname
};
teamgame.commentary = string.Join(",", strings.ToArray());
await testDb.SaveChangesAsync(); await testDb.SaveChangesAsync();
} user.Integral = user.Integral+10;
else await userManager.UpdateAsync(user);
{
var team_game = teamgame.commentary.Split(",").ToList();
team_game.Add(chinaname);
teamgame.commentary = string.Join(",", team_game.ToArray());
await testDb.SaveChangesAsync();
}
try try
{ {
await MessageManager.SendGroupMessageAsync("870248618", $"解说:\r\n{chinaname}\r\n选择了比赛:\r\n{teamgame.team1_name} VS {teamgame.team2_name}"); await MessageManager.SendGroupMessageAsync("870248618", $"解说:\r\n{chinaname}\r\n选择了比赛:\r\n{teamgame.team1_name} VS {teamgame.team2_name}");
@ -87,6 +79,21 @@ namespace asg_form.Controllers
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" }); return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });
} }
public class com_json
{
/// <summary>
///
/// </summary>
public int id { get; set; }
/// <summary>
/// 老恐龙
/// </summary>
public string chinaname { get; set; }
}
/// <summary> /// <summary>
/// 取消选班 /// 取消选班
@ -105,14 +112,17 @@ namespace asg_form.Controllers
TestDbContext testDb = new TestDbContext(); TestDbContext testDb = new TestDbContext();
string chinaname = user.chinaname; string chinaname = user.chinaname;
var teamgame = await testDb.team_Games.FirstAsync(a => a.id == gameid); var teamgame = await testDb.team_Games.FirstAsync(a => a.id == gameid);
var team_game = teamgame.commentary.Split(",").ToList(); var com = JsonConvert.DeserializeObject<List<com_json>>(teamgame.commentary);
team_game.Remove(chinaname); com.Remove(com.First(a => a.id == id.ToInt32()));
teamgame.commentary = string.Join(",", team_game.ToArray()); try{
if (teamgame.commentary=="") user.Integral = cut_value((long)user.Integral);
{ await userManager.UpdateAsync(user);
teamgame.commentary = "待公布";
} }
catch{
return BadRequest(new error_mb { code = 400, message = $"你的金钱无法满足你完成以下操作" });
}
teamgame.commentary = JsonConvert.SerializeObject(com);
await testDb.SaveChangesAsync(); await testDb.SaveChangesAsync();
return "成功"; return "成功";
} }
@ -125,12 +135,34 @@ namespace asg_form.Controllers
public async Task<ActionResult<string>> Search() public async Task<ActionResult<string>> Search()
{ {
TestDbContext testDb = new TestDbContext(); TestDbContext testDb = new TestDbContext();
var team= await testDb.team_Games.Select(a => new {a.id,a.commentary,a.opentime,a.team1_name,a.team2_name,a.belong}).ToListAsync(); var team = await testDb.team_Games.Select(a => new { a.id, a.commentary, a.opentime, a.team1_name, a.team2_name, a.belong }).ToListAsync();
var team1 = team.Where(a => a.commentary.Split(",").Length <= 1); var team1 = team.Where(a => a.commentary.Split(",").Length <= 1);
return JsonConvert.SerializeObject(team1); return JsonConvert.SerializeObject(team1);
} }
[Route("api/v1/com/Integral/ranking")]
[HttpPost]
[Authorize]
[ResponseCache(Duration = 60)]
public async Task<ActionResult<object>> ranking()
{
object user= await userManager.Users.OrderByDescending(a => a.Integral).Select(a=>new{a.Id,a.chinaname,a.Integral}).Take(10).ToListAsync();
return user;
}
public long cut_value(long value)
{
long _value = value;
value = value - 10;
if (value < 0)
{
throw new ArgumentException("你已经没钱啦!");
}
return value;
}
[Route("api/v1/com/")] [Route("api/v1/com/")]
[HttpGet] [HttpGet]
[Authorize] [Authorize]
@ -141,8 +173,8 @@ namespace asg_form.Controllers
if (user.officium == "Commentator") if (user.officium == "Commentator")
{ {
var chinaname = user.chinaname; var chinaname = user.chinaname;
TestDbContext testDb=new TestDbContext(); TestDbContext testDb = new TestDbContext();
int a= await testDb.team_Games.CountAsync(a => a.commentary.IndexOf(chinaname) >= 0); int a = await testDb.team_Games.CountAsync(a => a.commentary.IndexOf(id) >= 0);
return a; return a;
} }
return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" }); return BadRequest(new error_mb { code = 400, message = $"你是{user.officium},你不是解说,无法操作" });

View File

@ -1,4 +1,5 @@
 
using asg_form.Controllers.Store;
using asg_form.Model; using asg_form.Model;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -52,6 +53,22 @@ namespace asg_form.Controllers
} }
} }
public static class QueryableExtensions
{
public static IQueryable<T> Paginate<T>(this IQueryable<T> query, int pageIndex, int pageSize)
{
return query.Skip(pageIndex * pageSize).Take(pageSize);
}
}
class STOREConfig : IEntityTypeConfiguration<StoreinfoDB>
{
public void Configure(EntityTypeBuilder<StoreinfoDB> builder)
{
builder.ToTable("F_Store");
builder.HasOne<StoreDB>(c => c.Store).WithMany(a => a.buyer).IsRequired();
}
}
class newsConfig : IEntityTypeConfiguration<T_news> class newsConfig : IEntityTypeConfiguration<T_news>
{ {
@ -163,6 +180,8 @@ namespace asg_form.Controllers
public DbSet<Champion.T_Champion> Champions { get; set; } public DbSet<Champion.T_Champion> Champions { get; set; }
public DbSet<comform.com_form> com_Forms { get; set; } public DbSet<comform.com_form> com_Forms { get; set; }
public DbSet<T_Friend> T_Friends { get; set; } public DbSet<T_Friend> T_Friends { get; set; }
public DbSet<StoreDB> T_Store { get; set; }
public DbSet<StoreinfoDB> T_Storeinfo { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {

View File

@ -0,0 +1,25 @@
namespace asg_form.Controllers.Store
{
public class StoreDB
{
public long id { get; set; }
public string Name { get; set; }
public long Price { get; set; }
public string description { get; set; }
public string information { get; set; }
public string Type { get; set; }
public List<StoreinfoDB>? buyer { get; set; }=new List<StoreinfoDB>();
}
public class StoreinfoDB
{
public long id { get; set; }
public long buyerid { get; set; }
public StoreDB Store { get; set; }
public bool isVerification { get; set; }
}
}

View File

@ -0,0 +1,169 @@
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
{
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);
a = storeinfo;
await sb.SaveChangesAsync();
return Ok(storeinfo);
}
}
public 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<ActionResult<object>> GetStore()
{
using (TestDbContext sb = new TestDbContext())
{
var a= sb.T_Store.ToList().GroupBy(a => a.Type);
return Ok(a);
}
}
/// <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())
{
var a = new Store_record(null,null);
IQueryable<StoreinfoDB> b;
if (showVerification)
{
b = sb.T_Storeinfo;
}
else
{
b = sb.T_Storeinfo.Where(a=>a.isVerification==false);
}
if (search_id == null)
{
return Ok(await b.Paginate(pageindex, pagesize).ToListAsync()) ;
}
else
{
return Ok(await b.Where(a=>a.buyerid==search_id).Paginate(pageindex, pagesize).ToListAsync());
}
}
}
public record Store_record(long? allstort,List<StoreinfoDB>? Storeinfos);
[Route("api/v1/Store/Buy")]
[HttpGet]
[Authorize]
public async Task<ActionResult<object>> BuyStore(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())
{
var stort= await sb.T_Store.FindAsync(storeid);
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();
return Ok("购买成功,请前往背包查看");
}
catch
{
return BadRequest(new error_mb { code = 400, message = $"你的金钱无法满足你完成以下操作" });
}
}
}
}
}

View File

@ -21,7 +21,7 @@ using System.Text;
Console.WriteLine("\n _____ _________ ________ \n / _ \\ / _____// _____/ \n / /_\\ \\ \\_____ \\/ \\ ___ \n/ | \\/ \\ \\_\\ \\\n\\____|__ /_______ /\\______ /\n \\/ \\/ \\/ \n__________ __ ___________ .___\n\\______ \\_____ ____ | | __\\_ _____/ ____ __| _/\n | | _/\\__ \\ _/ ___\\| |/ / | __)_ / \\ / __ | \n | | \\ / __ \\\\ \\___| < | \\ | \\/ /_/ | \n |______ /(____ /\\___ >__|_ \\/_______ /___| /\\____ | \n \\/ \\/ \\/ \\/ \\/ \\/ \\/ "); Console.WriteLine("\n _____ _________ ________ \n / _ \\ / _____// _____/ \n / /_\\ \\ \\_____ \\/ \\ ___ \n/ | \\/ \\ \\_\\ \\\n\\____|__ /_______ /\\______ /\n \\/ \\/ \\/ \n__________ __ ___________ .___\n\\______ \\_____ ____ | | __\\_ _____/ ____ __| _/\n | | _/\\__ \\ _/ ___\\| |/ / | __)_ / \\ / __ | \n | | \\ / __ \\\\ \\___| < | \\ | \\/ /_/ | \n |______ /(____ /\\___ >__|_ \\/_______ /___| /\\____ | \n \\/ \\/ \\/ \\/ \\/ \\/ \\/ ");
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults(); //builder.AddServiceDefaults();
// Add services to the container. // Add services to the container.
@ -137,7 +137,7 @@ services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
var app = builder.Build(); var app = builder.Build();
app.MapDefaultEndpoints(); //app.MapDefaultEndpoints();
app.UseCors(); app.UseCors();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

View File

@ -1,4 +1,5 @@
 
using System.ComponentModel;
using asg_form.Controllers; using asg_form.Controllers;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
@ -7,6 +8,9 @@ namespace asg_form
public class User : IdentityUser<long> public class User : IdentityUser<long>
{ {
public string? UserBase64 { get; set; } public string? UserBase64 { get; set; }
[DefaultValue(0)]
public long? Integral { get; set; }
public DateTime CreationTime { get; set; } public DateTime CreationTime { get; set; }
public form? haveform { get; set; } public form? haveform { get; set; }
@ -14,7 +18,10 @@ namespace asg_form
public bool? isbooking { get; set; } public bool? isbooking { get; set; }
public string? chinaname { get; set; } public string? chinaname { get; set; }
public string? officium { get; set; } public string? officium { get; set; }
public int point { get; set; }
} }
public class Role : IdentityRole<long> public class Role : IdentityRole<long>

Binary file not shown.