public IActionResult CreateAuthorCollection(
            [FromBody] IEnumerable <UserForCreationDto> userCollection)
        {
            if (userCollection == null)
            {
                return(BadRequest());
            }

            var userEntities = Mapper.Map <IEnumerable <User> >(userCollection);

            foreach (var user in userEntities)
            {
                _libraryRepository.AddUser(user);
            }

            if (!_libraryRepository.Save())
            {
                throw new Exception("Create an author collection failed on save.");
            }

            var userCollectionToReturn = Mapper.Map <IEnumerable <UserDto> >(userEntities);
            var idAsStrings            = string.Join(",", userCollectionToReturn.Select(a => a.Id));

            return(CreatedAtRoute("GetUserCollection",
                                  new { ids = idAsStrings },
                                  userCollectionToReturn));
        }
        public IActionResult CreateUser([FromBody] UserForCreationDto user)
        {
            // Checks if the information object received is empty
            if (user == null)
            {
                return(BadRequest());
            }

            //Checks if the same username already exists.
            if (_libraryRepository.UserExists(user.Username))
            {
                return(new StatusCodeResult(StatusCodes.Status409Conflict));
            }

            user.Password = Helpers.GenerateHash.encryptPassword(user.Password);

            var userEntity = Mapper.Map <User>(user);

            _libraryRepository.AddUser(userEntity);

            if (!_libraryRepository.Save())
            {
                //Throwing exception here can hit the performance, but to simplify loggin it is better
                // than returning the status code from here.
                throw new Exception("Creating an author failed on save.");
            }

            var userToReturn = Mapper.Map <UserDto>(userEntity);

            var links = CreateLinksForUser(userToReturn.Id, null);

            var linkedResourceToReturn = user.ShapeData(null)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetUser",
                                  new { id = userToReturn.Id },
                                  linkedResourceToReturn));
        }
Exemple #3
0
 public void AddUser(PersonViewModel newUser)
 {
     _repo.AddUser(newUser);
 }