示例#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));
        }
示例#2
0
        public GetWordDto Post(string word)
        {
            // Create a new Word Entity
            var entity = new Word();

            // Assign WordName to the string word from user
            entity.WordName = word;

            // Using Dependency Injection
            // Add entity to database
            _context.Add(entity);
            // Save changes to database
            _context.SaveChanges();

            // Return ojbect including Id made by database
            return(new GetWordDto {
                Id = entity.Id, WordName = entity.WordName
            });
        }