// Create new thread public object Post(object modelParam) { try { var model = JsonConvert.DeserializeObject <QuizzmateMsgThreadModel>(modelParam.ToString()); if (model == null) { return(null); } QuizzmateMsgThread entity = null; model.MapToNew(out entity); var quizzmateThreadMember = new QuizzmateMsgThreadMember { UserId = _currentUser.Id }; entity.MsgThreadMembers.Add(quizzmateThreadMember); entity.SignalRGroupName = Guid.NewGuid().ToString(); _uow.QuizzmateMsgThreads.Add(entity); _uow.SaveChanges(); model = MappingUtil.Map <QuizzmateMsgThread, QuizzmateMsgThreadModel>(entity); if (IsMemberOrIsParent(model) == false) { return(null); } UpdateModel(model); return(model); } catch (Exception ex) { _svcContainer.LoggingSvc.Log(ex); return(null); } }
public object Post(object modelParam) { try { var model = JsonConvert.DeserializeObject <QuizzmateMsgThreadMemberModel>(modelParam.ToString()); if (model == null) { return(null); } var tmpEntity = _uow.QuizzmateMsgThreadMembers.GetAll() .Where(qm => qm.QuizzmateMsgThreadId == model.QuizzmateMsgThreadId && qm.UserId == model.UserId) .FirstOrDefault(); if (tmpEntity != null) { return(null); } QuizzmateMsgThreadMember entity = null; model.MapToNew(out entity); entity.HasNew = false; entity.NewCount = 0; _uow.QuizzmateMsgThreadMembers.Add(entity); _uow.SaveChanges(); model = MappingUtil.Map <QuizzmateMsgThreadMember, QuizzmateMsgThreadMemberModel>(entity); UpdateModel(model); return(model); } catch (Exception ex) { _svcContainer.LoggingSvc.Log(ex); return(null); } }
public object Get(int id) { try { var userId = id; var model = _uow.QuizzmateMsgThreads.GetAll() .Where(mt => mt.MsgThreadMembers.Select(tm => tm.UserId).Contains(userId) && mt.MsgThreadMembers.Select(tm => tm.UserId).Contains(_currentUser.Id)) .ProjectTo <QuizzmateMsgThreadModel>() .FirstOrDefault(); if (model == null) { var qtMember1 = new QuizzmateMsgThreadMember { HasNew = false, NewCount = 0, UserId = userId, UpdatedDate = DateTime.UtcNow }; var qtMember2 = new QuizzmateMsgThreadMember { HasNew = false, NewCount = 0, UserId = _currentUser.Id, UpdatedDate = DateTime.UtcNow }; var entity = new QuizzmateMsgThread { IsGroupMsg = false, GroupMessageName = "", SignalRGroupName = Guid.NewGuid().ToString(), IsDeleted = false }; entity.MsgThreadMembers.Add(qtMember1); entity.MsgThreadMembers.Add(qtMember2); _uow.QuizzmateMsgThreads.Add(entity); _uow.SaveChanges(); entity.MapToNew(out model); model.GroupMessageName = _uow.Users.GetAll() .Where(u => u.Id == userId) .Select(u => u.UserName) .FirstOrDefault(); } else { if (IsMemberOrIsParent(model) == false) { return(null); } var needCallSaveChanges = false; if (model.IsDeleted == true) { var entity = _uow.QuizzmateMsgThreads.GetById(model.Id); entity.IsDeleted = false; _uow.QuizzmateMsgThreads.Update(entity); needCallSaveChanges = true; } var messages = _uow.QuizzmateMsg1s.GetAll() .Where(qm => qm.QuizzmateMsgThreadId == model.Id) .OrderByDescending(qm => qm.PostedDate) .Take(20) .ProjectTo <QuizzmateMsgModel>() .ToList(); messages.Reverse(); model.Messages = messages; foreach (var item in model.MsgThreadMembers) { if (item.UserId == userId) { model.GroupMessageName = item.UserFullName; } if (item.UserId == _currentUser.Id) { QuizzmateMsgThreadMember msgThreadMemberEntity; item.MapToNew(out msgThreadMemberEntity); msgThreadMemberEntity.NewCount = 0; msgThreadMemberEntity.HasNew = false; _uow.QuizzmateMsgThreadMembers.Update(msgThreadMemberEntity); } } if (needCallSaveChanges) { _uow.SaveChanges(); } } UpdateModel(model); return(model); } catch (Exception ex) { _svcContainer.LoggingSvc.Log(ex); return(null); } }