public static List <Message> getAllMessages(int idUser) { if (_usersRepository.FindBy(x => x.Id == idUser && x.IsDeleted == false).FirstOrDefault() == null) { throw new UserWithThisIdDoesNotExists(); } List <Message> messages = new List <Message>(); foreach (int item in ChatRoomTools.getListIdChatRoomFromUser(idUser)) { messages.AddRange(_messagesRepository.FindBy(x => x.IsDeleted == false && x.IdChatRoom == item).ToList()); } return(messages); }
public IDtoOutObjects Seen(DtoInSeenMessage dtoInSeenMessage) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInSeenMessage.Token, dtoInSeenMessage.DeviceName)) { SeenMessage seenMessage = new SeenMessage(); seenMessage.SeenTime = System.DateTime.Now; Message message = _messagesRepository.FindBy(x => x.Id == dtoInSeenMessage.IdMessage && x.IsDeleted == false).FirstOrDefault(); if (message == null) { error.Exception = new MessageWithThisIdDoesNotExistsException(); error.Message = "Message with this id {" + dtoInSeenMessage.IdMessage + "} does not exists"; return(error); } if (!ChatRoomTools.getListIdChatRoomFromUser(TokenTools.getUserFromToken(dtoInSeenMessage.Token).Id).Contains(message.IdChatRoom)) { error.Exception = new MessageIsNotInYourListOfChatRooms(); error.Message = "Message is not in your list of chatRooms"; return(error); } seenMessage.IdMessage = dtoInSeenMessage.IdMessage; seenMessage.IdUser = TokenTools.getUserFromToken(dtoInSeenMessage.Token).Id; SeenMessage result = _seenMessagesRepository.Add(seenMessage); var config = new MapperConfiguration(cfg => { cfg.CreateMap <SeenMessage, DtoOutSeenMessage>(); }); IMapper mapper = config.CreateMapper(); DtoOutSeenMessage dtoOutSeenMessage = new DtoOutSeenMessage(); mapper.Map(result, dtoOutSeenMessage); return(dtoOutSeenMessage); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }