Exemple #1
0
        public async Task <ActionResult <UserDto> > CreateUser(CreateUserDto dto)
        {
            //add to the database
            var user = new User
            {
                FirstName = dto.FirstName,
                LastName  = dto.LastName,
                Email     = dto.Email,
                Username  = dto.Username,
                Password  = dto.Password
            };

            _context.Add(user);
            await _context.SaveChangesAsync();

            var entity = _context.Set <User>().Where(x => x.Username == dto.Username).FirstOrDefault();

            if (entity == null)
            {
                return(BadRequest());
            }

            var userDto = new UserDto
            {
                FirstName = entity.FirstName,
                LastName  = entity.LastName,
                Email     = entity.Email,
                // we dont want to return Passwords or Usernames or Id's
            };

            return(Ok(userDto));
        }
        public async Task <ActionResult <Person> > EditPerson(int id, CreatePersonDto dto)
        {
            var personToEdit = _context.Set <Person>().Find(id);

            personToEdit.Age       = dto.Age;
            personToEdit.FirstName = dto.FirstName;
            personToEdit.LastName  = dto.LastName;

            await _context.SaveChangesAsync();

            return(Ok(personToEdit));
        }