/// <summary> /// 移除主题, 默认软删除 /// </summary> /// <param name="id"></param> /// <param name="delete">是否彻底删除</param> /// <returns></returns> public async Task <Resp> Remove(int id, bool delete = false) { using var db = new MyForContext(); DB.Tables.Theme theme = await db.Themes.FirstOrDefaultAsync(t => t.Id == id); if (theme is null) { return(Resp.Fault(Resp.NONE, Theme.NONE_THEME)); } if (delete) { db.Themes.Remove(theme); } else { theme.State = (int)Theme.ThemeStates.Remove; } int suc = await db.SaveChangesAsync(); if (suc == 1) { return(Resp.Success(Resp.NONE, "")); } return(Resp.Fault(Resp.NONE, "失败")); }
/// <summary> /// 获取默认主题 /// </summary> public static Theme GetDefaultTheme() { const string KEY = "22f7b7b7-d674-47d5-89a4-eba97eebf3bc"; Theme defaultTheme = Cache.Get <Theme>(KEY); if (defaultTheme is null) { using var db = new MyForContext(); DB.Tables.Theme theme = db.Themes.AsNoTracking() .FirstOrDefault(t => t.Name == DEFAULT_THEME); // 没有默认主题就加一个 if (theme is null) { theme = new DB.Tables.Theme { Name = DEFAULT_THEME, MasterId = 1 }; db.Themes.Add(theme); db.SaveChanges(); } defaultTheme = new Theme(theme.Id); // 存缓存 _ = Cache.Set(KEY, defaultTheme, 300); } return(defaultTheme); }
public async Task <Resp> ModifyAsync(Models.UpdateTheme updateTheme) { (bool isValid, string msg) = updateTheme.IsValid(); if (!isValid) { return(Resp.Fault(Resp.NONE, msg)); } using var db = new MyForContext(); DB.Tables.Theme model = await db.Themes.FirstOrDefaultAsync(t => t.Id == Id); if (model is null) { return(Resp.Fault(Resp.NONE, NONE_THEME)); } model.Description = updateTheme.Description; if (updateTheme.IsChangeCover) { Files.File file = new Files.File(); model.CoverId = await file.SaveImageToDB(updateTheme.Cover); } int suc = await db.SaveChangesAsync(); if (suc == 1) { return(Resp.Success(Resp.NONE, "")); } return(Resp.Fault(Resp.NONE, "失败")); }
public static string GetName(int id) { string key = $"4d60b003-eb58-477c-a62d-de0580d486fa_{id}"; string name = Cache.Get <string>(key); if (name is null) { using var db = new MyForContext(); DB.Tables.Theme theme = db.Themes.AsNoTracking() .FirstOrDefault(t => t.Id == id); name = theme?.Name ?? ""; _ = Cache.Set(key, name, 10); } return(name); }
/// <summary> /// 通过主题名获取主题 ID, 若没有, 则返回默认主题 ID /// </summary> /// <param name="themeName"></param> /// <returns></returns> public static int GetIdByThemeName(string themeName) { themeName = themeName.Trim(); string key = $"6011a3cf-82a6-468a-b00b-ee76f886e926-{themeName}"; int id = Cache.Get <int>(key); // if (id == default) { using var db = new MyForContext(); DB.Tables.Theme theme = db.Themes.AsNoTracking().FirstOrDefault(t => t.Name == themeName); id = theme?.Id ?? GetDefaultTheme().Id; Cache.Set(key, id); } return(id); }
/// <summary> /// 启用主题 /// </summary> /// <param name="description">启用理由</param> /// <returns></returns> public async Task <Resp> EnabledAsync(string description) { using var db = new MyForContext(); DB.Tables.Theme theme = await db.Themes.FirstOrDefaultAsync(c => c.Id == Id); if (theme is null) { return(Resp.Fault(Resp.NONE, NONE_THEME)); } theme.State = (int)ThemeStates.Enabled; int suc = await db.SaveChangesAsync(); if (suc == 1) { return(Resp.Success()); } return(Resp.Fault(Resp.NONE, "启用失败")); }
/// <summary> /// 将题主转让给别人 /// </summary> /// <param name="receiver"></param> /// <returns></returns> public async Task <Resp> AssignAsync(int receiver) { using var db = new MyForContext(); DB.Tables.Theme model = await db.Themes.FirstOrDefaultAsync(t => t.Id == Id); if (model is null) { return(Resp.Fault(Resp.NONE, NONE_THEME)); } model.MasterId = receiver; int suc = await db.SaveChangesAsync(); if (suc == 1) { return(Resp.Success(Resp.NONE, "")); } return(Resp.Fault(Resp.NONE, "失败")); }
/// <summary> /// 新增主题 /// </summary> /// <returns></returns> public async Task <Resp> AddThemeAsync(Models.NewTheme newTheme) { (bool isValid, string msg) = newTheme.IsValid(); if (!isValid) { return(Resp.Fault(Resp.NONE, msg)); } using var db = new MyForContext(); bool exist = await db.Themes.AnyAsync(t => t.Name == newTheme.Name); if (exist) { return(Resp.Fault(Resp.NONE, "已存在的主题")); } Files.File file = new Files.File(); DB.Tables.Theme model = new DB.Tables.Theme { Name = newTheme.Name, Description = newTheme.Description, //CoverId = await file.SaveImageToDB(newTheme.Cover), MasterId = newTheme.CreateById, CreateById = newTheme.CreateById, State = (int)Theme.ThemeStates.ToAudit }; db.Themes.Add(model); int suc = await db.SaveChangesAsync(); if (suc == 1) { return(Resp.Success(Resp.NONE, "")); } return(Resp.Fault(Resp.NONE, "失败")); }
public async Task <Resp> GetDetailAsync() { using var db = new MyForContext(); DB.Tables.Theme theme = await db.Themes.AsNoTracking() .Include(t => t.Cover) .Include(t => t.Posts) .FirstOrDefaultAsync(a => a.Id == Id); if (theme is null) { return(Resp.Fault(Resp.NONE, NONE_THEME)); } Results.ThemeDetail detail = new Results.ThemeDetail { Name = theme.Name, Description = theme.Description, PostsCount = theme.Posts.Count, Creator = Clients.Client.GetName(theme.CreateById), CreateDate = theme.CreateDate.ToStandardString() }; return(Resp.Success(detail, "")); }