Exemplo n.º 1
0
        /// <summary>
        /// Deletes a userProfile from database AND from the login service.
        /// </summary>
        public bool DeleteUser(int userId)
        {
            Log.Information($"In USerProfileService.DeleteUser(): Request recieved with userId {userId}");

            // Delete user from loginService
            var loginDeleted = false;

            using (var loginService = new LoginServiceRef.LoginServiceClient())
            {
                if (!loginService.UserIdExist(userId))
                {
                    Log.Warning($"In USerProfileService.DeleteUser(): Unable to delete user with userId {userId}. UserId not found in loginService");
                }
                else
                {
                    loginDeleted = loginService.DeleteUser(userId);
                    if (!loginDeleted)
                    {
                        Log.Warning($"In USerProfileService.DeleteUser(): Unable to delete user with userId {userId}. loginService.DeleteUser(userId) returned false");
                    }
                }
            }
            // Delete userProfile from database
            var profileDeleted = DeleteUserProfile(userId);

            return(loginDeleted && profileDeleted);
        }
Exemplo n.º 2
0
        //----------------------------------------------------------------------------------------
        // Create & Update
        //----------------------------------------------------------------------------------------
        public User CreateUser(NewUser newUserFromWeb)
        {
            Log.Information($"In USerProfileService.CreateUser(): Request recieved with NewUser {JsonConvert.SerializeObject(newUserFromWeb)}");

            if (!_validator.ValidateNewUser(newUserFromWeb))
            {
                return(null);
            }

            int userId;

            using (var loginService = new LoginServiceRef.LoginServiceClient())
            {
                var newUser = new LoginServiceRef.NewUser
                {
                    Email     = newUserFromWeb.Email,
                    Firstname = newUserFromWeb.FirstName,
                    Surname   = newUserFromWeb.Surname,
                    Password  = newUserFromWeb.Password,
                    Username  = newUserFromWeb.Username
                };

                var returnUser = loginService.CreateUser(newUser);

                if (returnUser == null)
                {
                    // Logging that something went wrong when trying to save the new user in the other service
                    Log.Warning($"An attempt to create an account with the following values failed: {JsonConvert.SerializeObject(newUser)}");

                    return(null);
                }

                // Catches the newly created user's id
                userId = returnUser.ID;
            }

            using (db)
            {
                var userDb = new UserDb();
                db.UserDb.Add(userDb);

                _mapper.MapNewUserToModel(newUserFromWeb, userDb);
                userDb.UserId = userId;

                if (!UpdateDatabase())
                {
                    // Logging that something went wrong when trying to save the new user in the other service
                    Log.Warning($"An attempt to create a profile with the following values failed: {JsonConvert.SerializeObject(newUserFromWeb)}");
                    return(null);
                }

                // Return a User object so that one may add more profile data
                var user = _mapper.MapToWebService(userDb);
                return(user);
            }
        }