public async Task <ActionResult <MapCustomerAuthDTO> > Register(NewCustomerDTO newCustomer) { if (ModelState.IsValid) { var hashPassword = HashPassword(newCustomer.Password, 12); if (Verify(newCustomer.Password, hashPassword)) { var customerToRegister = new Customer { Name = newCustomer.Name, EmailAddress = newCustomer.EmailAddress, Role = Role.Customer, Password = hashPassword }; await _customerRepository.RegisterCustomer(customerToRegister); var mappedCustomer = CustomerMapper.MapToCustomerAuthDTO(customerToRegister); mappedCustomer.Token = GenerateJSONWebToken(mappedCustomer, customerToRegister.Role.GetDisplayName()); return(CreatedAtAction(nameof(GetById), new { id = customerToRegister.Id }, mappedCustomer)); } } return(BadRequest(newCustomer)); }
public IActionResult Create([FromBody] NewCustomerDTO model) { try { var customer = this.customerService.Create(model); return(Created("post", customer)); } catch (Exception e) { return(BadRequest(e.Message)); } }
/// <summary> /// Create a customer /// </summary> /// <param name="model">Data of the customer to be created.</param> /// <returns>Returns new customer or an appropriate error message.</returns> public CustomerDTO Create(NewCustomerDTO model) { var address = this.addressService.GetBaseForTest(model.AddressId); var customer = new Customer(); address.Customers.Add(customer); customer.FirstName = model.FirstName; customer.LastName = model.LastName; customer.Email = model.Email; customer.AddressId = model.AddressId; customer.CreatedOn = DateTime.UtcNow; this.dbContext.Customers.Add(customer); this.dbContext.SaveChanges(); var createdCustomer = FindCustomer(customer.Id); return(new CustomerDTO(createdCustomer)); }
public async Task <IHttpActionResult> NewCustomer(NewCustomerDTO newCustomerDTO) { string userName = User.Identity.Name; User user = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Product product = db.Products.Find(newCustomerDTO.ProductID); if (product == null) { return(NotFound()); } if ((product.CompanyID != user.Company.ID) && (product.TeamMembers.Where(teamMember => teamMember.UserID == user.Id && teamMember.CanEditTheProduct == true).SingleOrDefault() == null)) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } Customer customer = Mapper.Map <NewCustomerDTO, Customer>(newCustomerDTO); ProductUpdate productUpdate = new ProductUpdate(); customer.Product = product; customer = db.Customers.Add(customer); productUpdate.UserID = user.Id; productUpdate.User = user; productUpdate.ProductID = product.ID; productUpdate.Product = product; productUpdate.DateTime = DateTime.Now; productUpdate.UpdateType = UpdateType.ProductCustomerAdded; db.ProductUpdates.Add(productUpdate); await db.SaveChangesAsync(); return(Ok(Mapper.Map <Customer, CustomerDTO>(customer))); }