public bool AddKeyToGroup(string KeyId, string GroupKeyID, ref string msg) { var result = true; using (var _db = new CMS_Context()) { using (var trans = _db.Database.BeginTransaction()) { try { /* add new record */ var checkExist = _db.CMS_R_GroupKey_KeyWord.Where(o => o.KeyWordID == KeyId && o.GroupKeyID == GroupKeyID).FirstOrDefault(); if (checkExist != null) { if (checkExist.Status != (byte)Commons.EStatus.Active) { checkExist.Status = (byte)Commons.EStatus.Active; checkExist.UpdatedDate = DateTime.Now; } } else /* add new */ { var newGroupKey = new CMS_R_GroupKey_KeyWord() { ID = Guid.NewGuid().ToString(), GroupKeyID = GroupKeyID, KeyWordID = KeyId, Status = (byte)Commons.EStatus.Active, CreatedDate = DateTime.Now, UpdatedDate = DateTime.Now, }; _db.CMS_R_GroupKey_KeyWord.Add(newGroupKey); } _db.SaveChanges(); trans.Commit(); } catch (Exception ex) { msg = "Check connection, please!"; result = false; trans.Rollback(); } finally { _db.Dispose(); } } } return(result); }
public bool CreateOrUpdate(CMS_KeywordModels model, ref string msg) { var result = true; using (var _db = new CMS_Context()) { using (var trans = _db.Database.BeginTransaction()) { m_Semaphore.WaitOne(); try { if (string.IsNullOrEmpty(model.Id)) { /* check dup old key */ var key = model.KeySearch; var key2 = ""; if (key[key.Length - 1] == '/') { key2 = key.Substring(0, key.Length - 1); } else { key2 = key + "/"; } var checkDup = _db.CMS_KeyWord.Where(o => o.KeyWord == key || o.KeyWord == key2).FirstOrDefault(); if (checkDup == null) { /* get current seq */ var curSeq = _db.CMS_KeyWord.OrderByDescending(o => o.Sequence).Select(o => o.Sequence).FirstOrDefault(); /* add new record */ var dateTimeNow = DateTime.Now; var Id = Guid.NewGuid().ToString(); var newKey = new CMS_KeyWord() { ID = Id, KeyWord = model.KeySearch, Status = (byte)Commons.EStatus.Active, CreatedBy = model.CreatedBy, CreatedDate = dateTimeNow, UpdatedBy = model.CreatedBy, UpdatedDate = dateTimeNow, Sequence = ++curSeq, }; _db.CMS_KeyWord.Add(newKey); var newGroupKey = new CMS_R_GroupKey_KeyWord() { ID = Guid.NewGuid().ToString(), GroupKeyID = model.GroupID, KeyWordID = Id, Status = (byte)Commons.EStatus.Active, CreatedDate = DateTime.Now, UpdatedDate = DateTime.Now, }; _db.CMS_R_GroupKey_KeyWord.Add(newGroupKey); } else if (checkDup.Status != (byte)Commons.EStatus.Active) /* re-active old key */ { checkDup.Status = (byte)Commons.EStatus.Active; checkDup.UpdatedBy = model.CreatedBy; checkDup.UpdatedDate = DateTime.Now; } else /* duplicate key word */ { result = false; msg = "Duplicate key word."; } _db.SaveChanges(); trans.Commit(); } else { result = false; msg = "Unable to edit key word."; } } catch (Exception ex) { msg = "Check connection, please!"; result = false; trans.Rollback(); } finally { _db.Dispose(); m_Semaphore.Release(); } } } return(result); }