public IHttpActionResult AddConservation(ConservationBasicInfoDTO newConservation) { int receiverId = 0; int CurrentUser = 0; Conversation returnedConservation; try { using (var db = new UserDAL()) { CurrentUser = db.GetUserByUserNameOrEmail(User.Identity.Name).UserID; receiverId = db.GetUserByUserNameOrEmail(newConservation.ReceiverName).UserID; } //Add conservation var conservation = new Conversation { CreatorId = CurrentUser, ReceiverId = receiverId, Title = newConservation.Title, UpdatedTime = DateTime.Now, CreatedDate = DateTime.Now, Status = true, IsCreatorRead = true, IsReceiverRead = false, }; using (var db = new ConservationDAL()) { returnedConservation = db.AddNewConservation(conservation); } //Add First Message of Conservation var message = new Message { UserId = CurrentUser, ConservationId = returnedConservation.ConservationId, Content = newConservation.Content, CreatedDate = DateTime.Now, Status = true }; using (var db = new ConservationDAL()) { db.AddNewMessage(message); } } catch (Exception) { return(Redirect("/#/Error")); } return(Ok(new HTTPMessageDTO { Status = WsConstant.HttpMessageType.SUCCESS })); }
public List <ConservationBasicInfoDTO> GetAllConservationByUserIdAndStatus(string name, bool isRead) { int CurrenUser = 0; using (var db = new UserDAL()) { CurrenUser = db.GetUserByUserNameOrEmail(name).UserID; } List <ConservationBasicInfoDTO> ConservationList = new List <ConservationBasicInfoDTO>(); using (var db = new Ws_DataContext()) { var list = (from p in db.Conversation where (p.CreatorId == CurrenUser || p.ReceiverId == CurrenUser) select p).OrderByDescending(x => x.UpdatedTime).ToList(); foreach (var item in list) { if (CurrenUser == item.CreatorId && item.IsCreatorRead == isRead) { ConservationBasicInfoDTO current = new ConservationBasicInfoDTO(); //Set Image if (item.ReceiverId == CurrenUser) { current.AvatarUrl = item.Creator.User_Information.ProfileImage; current.CreatorName = item.Creator.UserName; } else { current.AvatarUrl = item.Receiver.User_Information.ProfileImage; current.CreatorName = item.Receiver.UserName; } //Set Time if (DateTime.Now.Subtract(item.UpdatedTime).TotalHours <= 24 && DateTime.Now.Subtract(item.UpdatedTime).TotalHours >= 1) { current.CreatedDate = Math.Round(DateTime.Now.Subtract(item.CreatedDate).TotalHours, 0) + " Tiếng cách đây"; } else if (DateTime.Now.Subtract(item.UpdatedTime).TotalHours > 24) { current.CreatedDate = item.UpdatedTime.ToString("H:mm:ss dd/MM/yy"); } else if (DateTime.Now.Subtract(item.CreatedDate).Minutes == 0) { current.CreatedDate = "Vừa xong"; } else { current.CreatedDate = DateTime.Now.Subtract(item.UpdatedTime).Minutes + " Phút cách đây"; } //Set other attributes current.Title = item.Title; current.ConservationId = item.ConservationId; ConservationList.Add(current); } if (CurrenUser == item.ReceiverId && item.IsReceiverRead == isRead) { ConservationBasicInfoDTO current = new ConservationBasicInfoDTO(); //Set Image if (item.ReceiverId == CurrenUser) { current.AvatarUrl = item.Creator.User_Information.ProfileImage; current.CreatorName = item.Creator.UserName; } else { current.AvatarUrl = item.Receiver.User_Information.ProfileImage; current.CreatorName = item.Receiver.UserName; } //Set Time if (DateTime.Now.Subtract(item.UpdatedTime).TotalHours <= 24 && DateTime.Now.Subtract(item.UpdatedTime).TotalHours >= 1) { current.CreatedDate = Math.Round(DateTime.Now.Subtract(item.CreatedDate).TotalHours, 0) + " Tiếng cách đây"; } else if (DateTime.Now.Subtract(item.UpdatedTime).TotalHours > 24) { current.CreatedDate = item.UpdatedTime.ToString("H:mm:ss dd/MM/yy"); } else if (DateTime.Now.Subtract(item.CreatedDate).Minutes == 0) { current.CreatedDate = "Vừa xong"; } else { current.CreatedDate = DateTime.Now.Subtract(item.UpdatedTime).Minutes + " Phút cách đây"; } //Set other attributes current.Title = item.Title; current.ConservationId = item.ConservationId; ConservationList.Add(current); } } return(ConservationList); } }