Ejemplo n.º 1
0
        public IActionResult Post([FromBody] UserViewModel user)
        {
            IUserResult result = this.UserService.Execute(new UserParameter()
            {
                User = user != null ? user.ToDataModel() : null
            });

            return(result.Status == UserStatus.Success ?
                   Ok(new AuthorizationViewModel(result.Authorization)) :
                   Unauthorized() as IActionResult);
        }
Ejemplo n.º 2
0
        public LoanResult(int id, IUserResult lender, IUserResult borrower, string message, DateTimeOffset startDate, DateTimeOffset endDate, IAddress address, IEnumerable <IOwnedBookResult> loanedBooks, string loanStatus)
        {
            if (id <= 0)
            {
                throw new ArgumentException("The id must be greater than 0.", nameof(id));
            }
            if (lender is null)
            {
                throw new ArgumentNullException(nameof(lender), "The lenderId must not be null.");
            }
            if (borrower is null)
            {
                throw new ArgumentNullException(nameof(borrower), "The borrowId must be not be null.");
            }
            if (startDate >= endDate)
            {
                throw new ArgumentException("The pickup date and time must be before the return date and time.");
            }
            if (address is null)
            {
                throw new ArgumentNullException(nameof(address), "The address must not be null.");
            }
            if (loanedBooks is null)
            {
                throw new ArgumentNullException(nameof(loanedBooks));
            }

            if (!loanedBooks.Any())
            {
                throw new ArgumentException("The number of loan books must be at least 1.");
            }

            if (loanedBooks.Distinct().Count() != loanedBooks.Count())
            {
                throw new ArgumentException("A loan cannot contain multiple of the same owned book.");
            }

            if (string.IsNullOrWhiteSpace(loanStatus))
            {
                throw new ArgumentException("The loan status must not be empty.");
            }

            Id        = id;
            Lender    = lender;
            Borrower  = borrower;
            Message   = message;
            StartDate = startDate;
            EndDate   = endDate;
            ExchangeLocationAddress = address;
            LoanedBooks             = new ReadOnlyCollection <IOwnedBookResult>(loanedBooks.ToList());
            Status = loanStatus;
        }
Ejemplo n.º 3
0
        public IUserResult UpdateItem(IUser model)
        {
            IUserResult result = null;

            if (string.IsNullOrEmpty(model.Id) == false)
            {
                //return userRepository.UpdateItem(model);
                return(null);
            }
            else
            {
                var exception = new Exception("Invalid User ID.");
                logger.Log(exception);

                throw exception;
            }
        }
Ejemplo n.º 4
0
        public IUserResult CreateUserAndAccount(IUser userDTO, string password)
        {
            IUserResult userCreateResult = null;

            try
            {
                if (_userService.EmailExists(userDTO.Email))
                {
                    //Can't create an account with the same email address.
                    userCreateResult = new UserFailureResult();
                    userCreateResult.AddData(string.Format("Email address '{0}' already exists.", userDTO.Email));
                }
                else
                {
                    var result = _userService.CreateItem(userDTO, password);

                    if (result.Result is UserSuccessResult)
                    {
                        var accountDTO = new AccountDTO();

                        accountDTO.UserId = ((UserDTO)result.Result.Item).Id;

                        if (string.IsNullOrEmpty(accountDTO.Name))
                        {
                            accountDTO.Name = "My Account";
                        }

                        var accountCreateResult = _accountService.CreateItem(accountDTO);

                        if (accountCreateResult is AccountSuccessResult)
                        {
                            Commit();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Rollback();
                userCreateResult = new UserFailureResult();
                userCreateResult.AddData(string.Format("Error occcured while attempting to create a new user/account: {0}.", e.Message));
            }

            return(userCreateResult);
        }
Ejemplo n.º 5
0
        public IUserResult GetItem(string key)
        {
            var         user   = users.Where(u => u.Id == key).FirstOrDefault();
            IUserResult result = null;

            if (user != null)
            {
                result = new UserSuccessResult()
                {
                    Item = user
                };
            }
            else
            {
                result = new UserFailureResult();
                result.AddData(new List <string> {
                    "Unable to locate user ID " + key
                });
            }

            return(result);
        }
Ejemplo n.º 6
0
        public IUserResult DeactivateUser(IUser userDTO)
        {
            IUserResult userDeactivationResult = null;

            try
            {
                userDTO.IsActive       = false;
                userDeactivationResult = _userService.UpdateItem(userDTO);

                if (userDeactivationResult is UserSuccessResult)
                {
                    //Deactivate any/all accounts for this user.
                    _accountService.DeactivateAllAccounts(userDTO.Id);
                }
            }
            catch (Exception e)
            {
                userDeactivationResult = new UserFailureResult();
                userDeactivationResult.AddData(string.Format("Error occurred while deactivating user: {0}.", e.Message));
            }

            return(userDeactivationResult);
        }
Ejemplo n.º 7
0
        public IUserResult UpdateItem(IUser model)
        {
            var         retrieveResult = GetItem(model.Id);
            IUserResult result         = null;

            if (retrieveResult is UserSuccessResult) //Normally you'd use a mapper here.
            {
                retrieveResult.Item.PhoneNumber = model.PhoneNumber;
                retrieveResult.Item.FirstName   = model.FirstName;
                retrieveResult.Item.LastName    = model.LastName;
                result = new UserSuccessResult()
                {
                    Item = retrieveResult.Item
                };
            }
            else
            {
                result = new UserFailureResult();
                ((List <string>)result.Data).Add("Unable to locate user ID " + model.Id);
            }

            return(result);
        }
Ejemplo n.º 8
0
        public EditableUserUpdate(IUserResult result) : base(result.Id)
        {
            Login     = result.Login;
            Lastname  = result.Lastname;
            Firstname = result.Firsname;
            Email     = result.Email;
            IsEnabled = result.IsEnabled;
            _role     = result.Role;
            Signature = result.Signature;

            switch (result.Role)
            {
            case UserRole.Admin:
                Role = "Administrateur";
                break;

            case UserRole.Manager:
                Role = "Gestionnaire formation";
                break;

            case UserRole.Operator:
                Role = "Service formation";
                break;

            case UserRole.Guest:
                Role = "Invité";
                break;

            case UserRole.Trainer:
                Role = "Formateur";
                break;

            default:
                Role = "Inconnu";
                break;
            }
        }
Ejemplo n.º 9
0
        public async Task <IUserResult> CreateItem(IUser userDTO, string password)
        {
            IUserResult result           = null;
            var         validationErrors = userValidator.Validate(userDTO);

            if (validationErrors.Count() == 0)
            {
                ((IoTHubUserIdentity)userDTO).UserName = userDTO.Email;

                var createResult = await userManager.CreateAsync((IoTHubUserIdentity)userDTO, password);

                if (createResult.Succeeded)
                {
                    result = new UserSuccessResult()
                    {
                        Item = userDTO
                    };
                }
                else
                {
                    result = new UserFailureResult()
                    {
                        Item = userDTO
                    };
                }
            }
            else
            {
                result = new UserFailureResult()
                {
                    Item = userDTO
                };
                result.AddData(validationErrors);
            }

            return(result);
        }