public IDtoOutObjects All(DtoInLogout dtoInLogout) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInLogout.Token, dtoInLogout.DeviceName)) { List <User> users = _usersRepostiory.FindBy(x => x.IsDeleted == false).ToList(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <User, DtoOutUser>(); }); IMapper mapper = config.CreateMapper(); List <DtoOutUser> list = new List <DtoOutUser>(); foreach (User item in users) { DtoOutUser dtoOutUser = new DtoOutUser(); mapper.Map(item, dtoOutUser); list.Add(dtoOutUser); } DtoOutAllUsers dtoOutAllUsers = new DtoOutAllUsers(); dtoOutAllUsers.dtoOutUsers = list; return(dtoOutAllUsers); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
public IDtoOutObjects Delete(DtoInAddFriend dtoInFriend) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInFriend.Token, dtoInFriend.DeviceName)) { User user = TokenTools.getUserFromToken(dtoInFriend.Token); Friend friend = _friendsRepository.FindBy(x => x.IdReciever == user.Id && x.IsDeleted == false && x.ObjectApplicant.Email == dtoInFriend.EmailReciever || x.ObjectReciever.Email == dtoInFriend.EmailReciever && x.IsDeleted == false && x.IdApplicant == user.Id).FirstOrDefault(); if (friend == null) { UserIsNotYourFriendException ex = new UserIsNotYourFriendException(); error.Exception = ex; return(error); } friend.IsDeleted = true; _friendsRepository.Edit(friend); _friendsRepository.Save(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <Friend, DtoOutFriend>(); }); IMapper mapper = config.CreateMapper(); DtoOutFriend dtoOutFriend = new DtoOutFriend(); mapper.Map(friend, dtoOutFriend); return(dtoOutFriend); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
public IDtoOutObjects EditUser(DtoInEditUser dtoInEditUser) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInEditUser.Token, dtoInEditUser.DeviceName)) { User user = TokenTools.getUserFromToken(dtoInEditUser.Token); user.BornDate = new DateTime(dtoInEditUser.Year, dtoInEditUser.Month, dtoInEditUser.Day); var configIn = new MapperConfiguration(cfg => { cfg.CreateMap <DtoInEditUser, User>(); }); IMapper mapperIn = configIn.CreateMapper(); mapperIn.Map(dtoInEditUser, user); _usersRepostiory.Edit(user); _usersRepostiory.Save(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <User, DtoOutUser>(); }); IMapper mapper = config.CreateMapper(); DtoOutUser dtoOutUser = new DtoOutUser(); mapper.Map(user, dtoOutUser); return(dtoOutUser); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
public IDtoOutObjects GetAllMessages(DtoInLogout dtoInLogout) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInLogout.Token, dtoInLogout.DeviceName)) { try { List <DtoOutMessageDetails> list = MessageTools.getMessageDetailsFromMessagesList(TokenTools.getUserFromToken(dtoInLogout.Token).Id); DtoOutAllMessages result = new DtoOutAllMessages(); result.dtoOutMessageDetails = list; return(result); } catch (Exception ex) { error.Exception = ex; return(error); } } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
public IDtoOutObjects GetById(DtoInGetById dtoInGetById) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInGetById.Token, dtoInGetById.DeviceName)) { User user = _usersRepostiory.FindBy(x => x.Id == dtoInGetById.Id && x.IsDeleted == false).FirstOrDefault(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <User, DtoOutUser>(); }); IMapper mapper = config.CreateMapper(); DtoOutUser dtoOutUser = new DtoOutUser(); mapper.Map(user, dtoOutUser); return(dtoOutUser); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
public IDtoOutObjects Accept(DtoInAddFriend dtoInFriend) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInFriend.Token, dtoInFriend.DeviceName)) { User acceptant = TokenTools.getUserFromToken(dtoInFriend.Token); Friend friend = _friendsRepository.FindBy(x => x.IdReciever == acceptant.Id && x.IsDeleted == false && x.ObjectApplicant.Email == dtoInFriend.EmailReciever).FirstOrDefault(); if (friend == null) { UserDoesNotAskedForFriendshipException ex = new UserDoesNotAskedForFriendshipException(); error.Exception = ex; return(error); } if (friend.Accepted == true) { YouAreAlreadyFriendsExceptions ex = new YouAreAlreadyFriendsExceptions(); error.Exception = ex; return(error); } friend.Accepted = true; _friendsRepository.Edit(friend); _friendsRepository.Save(); ChatRoomTools.Create(new List <User>() { acceptant, _usersRepository.FindBy(x => x.Email == dtoInFriend.EmailReciever).FirstOrDefault() }); var config = new MapperConfiguration(cfg => { cfg.CreateMap <Friend, DtoOutFriend>(); }); IMapper mapper = config.CreateMapper(); DtoOutFriend dtoOutFriend = new DtoOutFriend(); mapper.Map(friend, dtoOutFriend); return(dtoOutFriend); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
public IDtoOutObjects AddFriend(DtoInAddFriend dtoInFriend) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInFriend.Token, dtoInFriend.DeviceName)) { User userApplicant = TokenTools.getUserFromToken(dtoInFriend.Token); User userReciever = _usersRepository.FindBy(x => x.Email == dtoInFriend.EmailReciever && x.IsDeleted == false).FirstOrDefault(); if (userReciever == null) { UserWithThisEmailDoesntExistException ex = new UserWithThisEmailDoesntExistException(); error.Exception = ex; return(error); } if (FriendTools.areAlreadyFriends(userApplicant.Id, userReciever.Id)) { YouAreAlreadyFriendsExceptions ex = new YouAreAlreadyFriendsExceptions(); error.Exception = ex; return(error); } Friend friend = new Friend(); friend.ObjectApplicant = userApplicant; friend.ObjectReciever = userReciever; _friendsRepository.Add(friend); _friendsRepository.Save(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <Friend, DtoOutFriend>(); }); IMapper mapper = config.CreateMapper(); DtoOutFriend dtoOutFriend = new DtoOutFriend(); mapper.Map(friend, dtoOutFriend); return(dtoOutFriend); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
public IDtoOutObjects ChangePassword(DtoInChangePassword dtoInChangePassword) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInChangePassword.Token, dtoInChangePassword.DeviceName)) { User user = TokenTools.getUserFromToken(dtoInChangePassword.Token); Credential credential = _credentialsRepository.FindBy(x => x.IdUser == user.Id && x.IsDeleted == false && x.ObjectUser.IsDeleted == false).FirstOrDefault(); // toto zanmená že každý user může mít jen jedny credentials credential.Password = dtoInChangePassword.Password; _credentialsRepository.Edit(credential); _credentialsRepository.Save(); DtoOutComplete dtoOutComplete = new DtoOutComplete(); dtoOutComplete.Completed = true; return(dtoOutComplete); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
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); } }
public IDtoOutObjects Send(DtoInMessage dtoInMessage) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInMessage.Token, dtoInMessage.DeviceName)) { Message message = null; try { message = new Message(dtoInMessage); } catch (ObjectIsNotValidException ex) { error.Exception = ex; error.Message = "this chatroom does not exists"; return(error); } if (ChatRoomTools.getListIdChatRoomFromUser(TokenTools.getUserFromToken(dtoInMessage.Token).Id).Contains(message.IdChatRoom)) { error.Exception = new MessageIsNotInYourListOfChatRooms(); error.Message = "Message is not in your list of chatRooms"; return(error); } Message result = _messagesRepository.Add(message); _messagesRepository.Save(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <Message, DtoOutMessage>(); }); IMapper mapper = config.CreateMapper(); DtoOutMessage dtoOutMessage = new DtoOutMessage(); mapper.Map(result, dtoOutMessage); return(dtoOutMessage); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
public IDtoOutObjects Logout(DtoInLogout dtoInLogout) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInLogout.Token, dtoInLogout.DeviceName)) { Token token = TokenTools.getTokenObjectFromString(dtoInLogout.Token); token.IsDeleted = true; tokensRepository.Edit(token); tokensRepository.Save(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <Token, DtoOutToken>(); }); IMapper mapper = config.CreateMapper(); DtoOutToken dtoOutToken = new DtoOutToken(); mapper.Map(token, dtoOutToken); return(dtoOutToken); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
public IDtoOutObjects DeleteUser(DtoInLogout dtoInLogout) { DtoOutError error = new DtoOutError(); if (TokenTools.Authentication(dtoInLogout.Token, dtoInLogout.DeviceName)) { User user = TokenTools.getUserFromToken(dtoInLogout.Token); user.IsDeleted = true; _usersRepostiory.Edit(user); _usersRepostiory.Save(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <User, DtoOutUser>(); }); IMapper mapper = config.CreateMapper(); DtoOutUser dtoOutUser = new DtoOutUser(); mapper.Map(user, dtoOutUser); return(dtoOutUser); } else { NotAuthenticatedException ex = new NotAuthenticatedException(); error.Exception = ex; return(error); } }
public bool Authenticate(DtoInMessage Token) { return(TokenTools.Authentication(Token.Token, Token.DeviceName)); }