public async Task <ActionResult> GetCustomer(GetCustomerViewModel model, CancellationToken cancellationToken) { EnsureModalStateIsValid(); var request = Mapper.Map <GetCustomerViewModel, GetCustomerRequest>(model); var response = await Mediator.Send(request, cancellationToken); return(await HandleResponse(response, cancellationToken)); }
public async Task <GetCustomerViewModel> GetCustomerInfo(string customerId) { //var customer = await _context.Users.FindAsync(customerId); var customer = await _context.Users.Include(c => c.DeliveryAddress) .Include(c => c.InvoiceAddress) .Include(c => c.Orders) .Include(c => c.InvoiceAddress.City) .Include(c => c.DeliveryAddress.City) .FirstOrDefaultAsync(c => c.Id == customerId); var customerInfo = new GetCustomerViewModel(customer); return(customerInfo); }
/* * Get CustomerViewModel for the required Customer */ public async Task <GetCustomerViewModel> GetCustomerViewModel(Customer customer) { if (customer == null) { return(null); } var basketViewModel = await _basketService.GetBasketViewModel(customer); var customerViewModel = new GetCustomerViewModel { Id = customer.Id, Name = customer.Name, Basket = basketViewModel }; return(customerViewModel); }
public async Task <ActionResult <GetCustomerViewModel> > PutCustomer([Microsoft.AspNetCore.Mvc.FromBody] PutCustomerViewModel putCustomerViewModel, Guid?customerId = null) { string id; var userId = User.FindFirst(ClaimTypes.NameIdentifier); if (customerId == null) { id = userId.Value; } else { var role = await _context.UserRoles.FirstOrDefaultAsync(r => r.UserId == userId.Value); if (role.RoleId != "2" && role.RoleId != "3") { return(Unauthorized("Only Employess or Admin is allowed to change other customers")); } id = customerId.ToString(); } if (putCustomerViewModel.Email != null) { var userwithEmail = await _context.Users.FirstOrDefaultAsync(p => p.Email == putCustomerViewModel.Email); if (userwithEmail != null && userwithEmail.Id != id) { return(Conflict("Email already in use")); } } var customer = await _context.Users //.Include(c => c.DeliveryAddress) //.Include(c => c.InvoiceAddress) //.Include(c => c.Orders) //.Include(c => c.InvoiceAddress.City) //.Include(c => c.DeliveryAddress.City) .FirstOrDefaultAsync(c => c.Id == id); customer.FirstName = putCustomerViewModel.FirstName ?? customer.FirstName; customer.LastName = putCustomerViewModel.LastName ?? customer.LastName; customer.PhoneNumber = putCustomerViewModel.PhoneNumber ?? customer.PhoneNumber; customer.Email = putCustomerViewModel.Email ?? customer.Email; customer.UserName = putCustomerViewModel.Email ?? customer.UserName; customer.NormalizedEmail = putCustomerViewModel.Email.ToUpper() ?? customer.NormalizedEmail; customer.NormalizedUserName = putCustomerViewModel.Email.ToUpper() ?? customer.NormalizedUserName; customer.IsDisabled = putCustomerViewModel.IsDisabled ?? customer.IsDisabled; _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (Exception e) { //TODO: proper error handling Console.WriteLine(e); throw; } var DeliveryAddress = await _context.Address .Include(a => a.City) .FirstOrDefaultAsync(a => a.AddressId == customer.DeliveryAddressId); if (putCustomerViewModel.DeliveryAddress != null) { DeliveryAddress.State = putCustomerViewModel.DeliveryAddress.State ?? customer.DeliveryAddress.State; DeliveryAddress.Country = putCustomerViewModel.DeliveryAddress.Country ?? DeliveryAddress.Country; DeliveryAddress.Street = putCustomerViewModel.DeliveryAddress.Street ?? DeliveryAddress.Street; DeliveryAddress.StreetNumber = putCustomerViewModel.DeliveryAddress.StreetNumber ?? DeliveryAddress.StreetNumber; DeliveryAddress.City.Name = putCustomerViewModel.DeliveryAddress.CityName ?? DeliveryAddress.City.Name; DeliveryAddress.City.PostalCode = putCustomerViewModel.DeliveryAddress.PostalCode ?? DeliveryAddress.City.PostalCode; } _context.Entry(DeliveryAddress).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (Exception e) { //TODO: proper error handling Console.WriteLine(e); throw; } _context.Entry(DeliveryAddress).State = EntityState.Detached; var InvoiceAddress = await _context.Address .Include(a => a.City) .FirstOrDefaultAsync(a => a.AddressId == customer.InvoiceAddressId); if (putCustomerViewModel.InvoiceAddress != null) { InvoiceAddress.State = putCustomerViewModel.InvoiceAddress.State ?? InvoiceAddress.State; InvoiceAddress.Country = putCustomerViewModel.InvoiceAddress.Country ?? InvoiceAddress.Country; InvoiceAddress.Street = putCustomerViewModel.InvoiceAddress.Street ?? InvoiceAddress.Street; InvoiceAddress.StreetNumber = putCustomerViewModel.InvoiceAddress.StreetNumber ?? InvoiceAddress.StreetNumber; InvoiceAddress.City.Name = putCustomerViewModel.InvoiceAddress.CityName ?? InvoiceAddress.City.Name; InvoiceAddress.City.PostalCode = putCustomerViewModel.InvoiceAddress.PostalCode ?? InvoiceAddress.City.PostalCode; } _context.Entry(InvoiceAddress).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (Exception e) { //TODO: proper error handling Console.WriteLine(e); throw; } var getCustomer = await _context.Users .Include(c => c.DeliveryAddress) .Include(c => c.InvoiceAddress) .Include(c => c.Orders) .Include(c => c.InvoiceAddress.City) .Include(c => c.DeliveryAddress.City) .FirstOrDefaultAsync(c => c.Id == id); var getCustomerViewModel = new GetCustomerViewModel(getCustomer); return(Ok(getCustomerViewModel)); }