Exemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] UserModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var user = await GetUserByEmail(model.EmailAddress);

                if (user != null)
                {
                    _logger.LogInformation($"User with email '{model.EmailAddress}' already exists");

                    ModelState.AddModelError(nameof(model.EmailAddress), "Email address already exists");
                    return(BadRequest(ModelState));
                }

                user              = new User();
                user.FirstName    = model.FirstName;
                user.LastName     = model.LastName;
                user.EmailAddress = model.EmailAddress;

                _dbContext.Users.Add(user);
                await _dbContext.SaveChangesAsync();

                model.Id = user.Id;

                return(Created(Url.Action(nameof(GetById), new { id = user.Id }), model));
            }catch (Exception ex)
            {
                _logger.LogError(ex, $"Error creating user with email: {model.EmailAddress}");
                return(StatusCode(500, "Error creating user"));
            }
        }
Exemplo n.º 2
0
 public async Task CreateProduct(Product product)
 {
     _snackDbContext.Products.Add(product);
     await _snackDbContext.SaveChangesAsync();
 }