示例#1
0
        public async Task <IActionResult> Put(CustomerDto customer)
        {
            // If the caller doesnt provide id we try getting it from claims
            customer.Id ??= User.Claims.FirstOrDefault(c => c.Type.Equals(JwtClaimTypes.Name))?.Value;

            if (customer.Id == null)
            {
                logger.LogDebug("No id provided or found for the User");
                return(BadRequest("No Id provided"));
            }

            var dbCustomer = await dbContext.Customers.FindAsync(customer.Id);

            if (dbCustomer == null)
            {
                logger.LogDebug($"No Customer found in db for id: {customer.Id}");
                return(NotFound("Customer not found"));
            }

            dbContext.Attach(dbCustomer);

            dbCustomer.RegistrationNumber = customer.RegistrationNumber;

            await dbContext.SaveChangesAsync();

            logger.LogDebug($"Customer updated with id: {customer.Id}");

            return(Ok(dbCustomer));
        }