Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        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);
            }
        }
Esempio n. 4
0
        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);
            }
        }
Esempio n. 5
0
        public JsonResult <IDtoOutObjects> Send(DtoInMessage dtoInMessage)
        {
            if (ModelState.IsValid)
            {
                return(Json(_messageModel.Send(dtoInMessage)));
            }

            DtoOutError error = new DtoOutError();

            error.Exception = new ObjectIsNotValidException("Message");
            error.Message   = "message is not valid";
            return(Json((IDtoOutObjects)error));
        }
Esempio n. 6
0
        public JsonResult <IDtoOutObjects> ChangePassword(DtoInChangePassword dtoInChangePassword)
        {
            if (ModelState.IsValid)
            {
                return(Json(_credentialModel.ChangePassword(dtoInChangePassword)));
            }

            DtoOutError error = new DtoOutError();

            error.Exception = new ObjectIsNotValidException("CredentialDto");
            error.Message   = "CredentialDto is not valid";
            return(Json((IDtoOutObjects)error));
        }
Esempio n. 7
0
        public JsonResult <IDtoOutObjects> New(DtoInGetNewMessages dtoInGetNewMessages)
        {
            if (ModelState.IsValid)
            {
                return(Json(_messageModel.GetNewMessages(dtoInGetNewMessages)));
            }

            DtoOutError error = new DtoOutError();

            error.Exception = new ObjectIsNotValidException("SeenMessage");
            error.Message   = "Seenmessage is not valid";
            return(Json((IDtoOutObjects)error));
        }
Esempio n. 8
0
        public JsonResult <IDtoOutObjects> GetByIdChatRoom(DtoInGetMessagesFromChatRoom dtoInGetMessagesFromChatRoom)
        {
            if (ModelState.IsValid)
            {
                return(Json(_messageModel.GetByIdChatRoom(dtoInGetMessagesFromChatRoom)));
            }

            DtoOutError error = new DtoOutError();

            error.Exception = new ObjectIsNotValidException("DtoInGetMessagesFromChatRoom");
            error.Message   = "DtoInGetMessagesFromChatRoom is not valid";
            return(Json((IDtoOutObjects)error));
        }
Esempio n. 9
0
        public JsonResult <IDtoOutObjects> Delete(DtoInAddFriend dtoInFriend)
        {
            if (ModelState.IsValid)
            {
                return(Json(friendModel.Delete(dtoInFriend)));
            }

            DtoOutError error = new DtoOutError();

            error.Exception = new ObjectIsNotValidException("AddFriend");
            error.Message   = "Object accept friend is not valid";
            return(Json((IDtoOutObjects)error));
        }
Esempio n. 10
0
        public JsonResult <IDtoOutObjects> Edit(DtoInEditUser dtoInEditUser)
        {
            // Jak udělat authenticate
            //Ukázkové použití validačního frameworku
            if (ModelState.IsValid)
            {
                return(Json(_userModel.EditUser(dtoInEditUser)));
            }

            DtoOutError error = new DtoOutError();

            error.Exception = new ObjectIsNotValidException("dtoInEditUser");
            error.Message   = "dtoInEditUser is not valid";
            return(Json((IDtoOutObjects)error));
        }
Esempio n. 11
0
 public JsonResult <IDtoOutObjects> Logout(DtoInLogout dtoInLogout)
 {
     if (ModelState.IsValid)
     {
         return(Json(loginModel.Logout(dtoInLogout)));
     }
     else
     {
         DtoOutError error = new DtoOutError();
         CredentialAreNotValidException ex = new CredentialAreNotValidException();
         error.Exception = ex;
         error.Message   = "Credentials are not valid";
         return(Json((IDtoOutObjects)error));
     }
 }
Esempio n. 12
0
 public JsonResult <IDtoOutObjects> Login(DtoInCredential dtoInCredential)
 {
     if (ModelState.IsValid)
     {
         return(Json((IDtoOutObjects)loginModel.Login(dtoInCredential.LoginName, dtoInCredential.Password, dtoInCredential.DeviceName)));
     }
     else
     {
         DtoOutError error = new DtoOutError();
         CredentialAreNotValidException ex = new CredentialAreNotValidException();
         error.Exception = ex;
         error.Message   = "Credentials are not valid";
         return(Json((IDtoOutObjects)error));
     }
 }
Esempio n. 13
0
        public JsonResult <IDtoOutObjects> Create(DtoInUser user)
        {
            // Jak udělat authenticate
            //Ukázkové použití validačního frameworku
            if (ModelState.IsValid)
            {
                return(Json(_userModel.CreateUser(user)));
            }

            DtoOutError error = new DtoOutError();

            error.Exception = new UserIsNotValidException();
            error.Message   = "user is not valid";
            return(Json((IDtoOutObjects)error));
        }
Esempio n. 14
0
        public JsonResult <IDtoOutObjects> Save(DtoInCredential dtoInCredential)
        {
            LoginModel loginModel = new LoginModel();

            try
            {
                return(Json((IDtoOutObjects)loginModel.Login(dtoInCredential.LoginName, dtoInCredential.Password, dtoInCredential.DeviceName)));
            }
            catch (Exception ex)
            {
                DtoOutError error = new DtoOutError();
                error.Exception = ex;
                error.Message   = "user is not valid";
                return(Json((IDtoOutObjects)error));
            }
        }
Esempio n. 15
0
        public IDtoOutObjects CreateUser(DtoInUser dtoInUser)
        {
            string  deviceName = dtoInUser.DeviceName;
            User    user       = new User();
            var     configIn   = new MapperConfiguration(cfg => { cfg.CreateMap <DtoInUser, User>(); });
            IMapper mapperIn   = configIn.CreateMapper();

            mapperIn.Map(dtoInUser, user);
            user.BornDate = new DateTime(dtoInUser.Year, dtoInUser.Month, dtoInUser.Day);
            try
            {
                if (!UserExists(user))
                {
                    Credential c = new Credential(user.Email, Guid.NewGuid().ToString(), user);

                    Credential credential = credentialsRepository.Add(c);
                    credentialsRepository.Save();
                    User u = _usersRepostiory.FindBy(x => x.Id == user.Id && x.IsDeleted == false).FirstOrDefault();
                    #region createMapperUser
                    var     config = new MapperConfiguration(cfg => { cfg.CreateMap <User, DtoOutUser>(); });
                    IMapper mapper = config.CreateMapper();
                    #endregion
                    DtoOutUser dtoOutUser = new DtoOutUser();
                    mapper.Map(u, dtoOutUser);

                    dtoOutUser.TokenString = TokenTools.CreateToken(u, deviceName).TokenString;
                    return(dtoOutUser);
                }
                else
                {
                    DtoOutError error = new DtoOutError();
                    error.Exception = new DuplicateObjectInDatabaseException("User");
                    error.Message   = "This user is already created";
                    return(error);
                }
            }
            catch (Exception ex)
            {
                DtoOutError error = new DtoOutError();
                error.Exception = ex;
                error.Message   = ex.Message;
                return(error);
            }
        }
Esempio n. 16
0
        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);
            }
        }
Esempio n. 17
0
        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);
            }
        }
Esempio n. 18
0
        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);
            }
        }
Esempio n. 19
0
        public IDtoOutObjects Login(string loginName, string password, string deviceName)
        {
            Credential credential = _credentialRepository.FindBy(x => x.LoginName == loginName && x.IsDeleted == false && x.ObjectUser.IsDeleted == false).FirstOrDefault();

            if (credential != null && credential.Password == password)
            {
                Token       t           = TokenTools.CreateToken(credential.ObjectUser, deviceName);
                var         config      = new MapperConfiguration(cfg => { cfg.CreateMap <Token, DtoOutToken>(); });
                IMapper     mapper      = config.CreateMapper();
                DtoOutToken dtoOutToken = new DtoOutToken();
                mapper.Map(t, dtoOutToken);
                dtoOutToken.IdUser = TokenTools.getUserFromToken(dtoOutToken.TokenString).Id;
                return(dtoOutToken);
            }
            else
            {
                DtoOutError error = new DtoOutError();
                error.Exception = new CredentialAreNotValidException();
                error.Message   = "Credentials are not assign to account";
                return(error);
            }
        }
Esempio n. 20
0
        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);
            }
        }
Esempio n. 21
0
        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);
            }
        }
Esempio n. 22
0
        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);
            }
        }
Esempio n. 23
0
        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);
            }
        }
Esempio n. 24
0
        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);
            }
        }