public async Task Create(Template entry) { var code = entry.Code.ToUpper(); var appCode = entry.AppCode.ToUpper(); var lang = entry.Lang; using (var db = new Entities()) { if (await this.IsRepeat(db, entry.Code, entry.AppCode, entry.Lang)) throw new DataRepeatException<Template>(entry, t => t.Code, t => t.AppCode, t => t.Lang); else { var template = new Template() { Code = code, AppCode = appCode, Lang = lang, Ctx = entry.Ctx, Subject = entry.Subject, IsDefault = entry.IsDefault, MsgType = entry.MsgType }; this.SetCreateInfo(template); db.Templates.Add(template); this.Errors = db.GetErrors(); if (!this.HasError) await db.SaveChangesAsync(); } } }
public async Task Edit(int id, Template entry) { using (var db = new Entities()) { if (!await this.IsRepeat(db, entry.Code, entry.AppCode, entry.Lang, id)) { var ex = await db.Templates.FirstOrDefaultAsync(t => t.ID == id && !t.IsDeleted); if (ex != null) { entry.CopyToOnly(ex, p => p.Code, p => p.AppCode, p => p.Lang, p => p.MsgType, p => p.Ctx, p => p.Subject, p => p.IsDefault ); this.SetModifyInfo(ex); this.Errors = db.GetErrors(); if (!this.HasError) await db.SaveChangesAsync(); } } else { throw new DataRepeatException<Template>(entry, t => t.AppCode, t => t.Code, t => t.Lang); } } }