示例#1
0
        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();
                }
            }
        }
示例#2
0
 public async Task<TxtMessage> GetTxtMsg(int msgID, decimal receiverID, bool setReaded = false) {
     using (var db = new Entities()) {
         var data = await db.TxtMessages.FirstOrDefaultAsync(t => !t.IsDeleted && t.ReceiverID == receiverID && t.ID == msgID);
         if (setReaded) {
             data.Readed = true;
             this.SetModifyInfo(data);
             await db.SaveChangesAsync();
         }
         return data;
     }
 }
示例#3
0
 public async Task<bool> SetTxtMsgReaded(int msgID) {
     using (var db = new Entities()) {
         var c = await db.TxtMessages.FirstOrDefaultAsync(t => t.ID == msgID && !t.IsDeleted && !t.Readed);
         if (c != null) {
             c.Readed = true;
             this.SetModifyInfo(c);
             await db.SaveChangesAsync();
             return true;
         }
     }
     return false;
 }
示例#4
0
        public async Task<IEnumerable<TxtMessage>> GetTxtMsg(decimal receiverID, Pager pager = null, bool onlyUnread = true) {
            using (var db = new Entities()) {
                var query = db.TxtMessages.Where(t => t.ReceiverID == receiverID && !t.IsDeleted);
                if (onlyUnread) {
                    query = query.Where(t => !t.Readed);
                }

                if (pager == null)
                    pager = new Pager();

                return await query.OrderByDescending(t => t.CreateOn)
                    .DoPage(pager)
                    .ToListAsync();
            }
        }
示例#5
0
        public async Task<Template> GetByCode(string code, string appCode, MsgTypes msgType, Langs? lang) {
            code = code.ToUpper().Trim();
            appCode = appCode.ToUpper();

            using (var db = new Entities()) {
                var query = db.Templates.Where(t =>
                    !t.IsDeleted
                    && t.Code.ToUpper() == code
                    && t.AppCode.ToUpper() == appCode
                    && t.MsgType == msgType
                    && ((lang != null && t.Lang == lang.Value) || (lang == null && t.IsDefault))
                    );

                return await query.FirstOrDefaultAsync();
            }
        }
示例#6
0
 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);
         }
     }
 }
示例#7
0
 public async Task<bool> DeleteTxtMsg(int msgID, decimal receiverID) {
     using (var db = new Entities()) {
         var data = await db.TxtMessages.FirstOrDefaultAsync(t => t.ID == msgID && t.ReceiverID == receiverID && !t.IsDeleted);
         if (data != null) {
             data.IsDeleted = true;
             this.SetModifyInfo(data);
             await db.SaveChangesAsync();
             return true;
         }
     }
     return false;
 }
示例#8
0
 public async Task<int> GetUnReadTxtMsgCount(decimal receiverID) {
     using (var db = new Entities()) {
         return await db.TxtMessages.CountAsync(t => t.ReceiverID == receiverID && !t.IsDeleted && !t.Readed);
     }
 }
示例#9
0
 public async Task<bool> Delete(decimal id) {
     using (var db = new Entities()) {
         var ex = db.Templates.FirstOrDefault(t => t.ID == id && !t.IsDeleted);
         if (ex != null) {
             ex.IsDeleted = true;
             this.SetModifyInfo(ex);
             await db.SaveChangesAsync();
             return true;
         }
     }
     return false;
 }
示例#10
0
 public async Task<Template> GetBySeq(decimal id) {
     using (var db = new Entities()) {
         return await db.Templates.FirstOrDefaultAsync(t => t.ID == id);
     }
 }
示例#11
0
        private async Task<bool> IsRepeat(Entities db, string code, string appCode, Langs lang, int? id = null) {
            id = id ?? -1;
            code = code.ToUpper().Trim();
            appCode = appCode.ToUpper().Trim();

            return await db.Templates.AnyAsync(t =>
                !t.IsDeleted
                && t.ID != id

                && t.Code.ToUpper() == code
                && t.AppCode.ToUpper() == appCode
                && t.Lang == lang);
        }
示例#12
0
 public async Task<IEnumerable<Template>> Search(TemplateSearchCondition cond) {
     using (var db = new Entities()) {
         var query = cond.Filter(db.Templates.Where(t => !t.IsDeleted));
         return await query
             .OrderBy(m => m.Code)
             .DoPage(cond.Pager).ToListAsync();
     }
 }