public override ResponseBase ExecuteRequest() { CustomerDTO CustomerDTO = new CustomerDTO(); AddCustomerResponse responseObject = new AddCustomerResponse(); try { Customer CUSTOMER = new Customer(); using (CustomerDemoEntities ctx = new CustomerDemoEntities()) { var _customer = ctx.Customers.Where(c => c.Name == customer.Name).FirstOrDefault(); if (_customer == null) { if (customer.PhoneNumbers.Count > 0) { List <PhoneNumber> phoneNumbers = new List <PhoneNumber>(); for (int i = 0; i < customer.PhoneNumbers.Count; i++) { PhoneNumber phoneNumber = new PhoneNumber(); phoneNumber.Number = customer.PhoneNumbers[i].Number; phoneNumbers.Add(phoneNumber); ctx.PhoneNumbers.Add(phoneNumber); } CUSTOMER.PhoneNumbers = phoneNumbers; } CUSTOMER.Name = customer.Name; CUSTOMER.BirthDate = customer.BirthDate; CUSTOMER.Gender = customer.Gender; CUSTOMER.Email = customer.Email; CUSTOMER.Address = customer.Address; CUSTOMER.Notes = customer.Notes; ctx.Customers.Add(CUSTOMER); ctx.SaveChanges(); customer.ID = CUSTOMER.ID; CustomerDTO = ConfigMapper.Map <Customer, CustomerDTO>(CUSTOMER); responseObject.Message = "Customer Added Successufully"; responseObject.IsSuccess = true; responseObject.customer = CustomerDTO; } else { responseObject.IsSuccess = false; responseObject.Message = "Customer already exist"; } } } catch (Exception e) { responseObject.IsSuccess = false; responseObject.Message = e.Message; } return(responseObject); }
public IAddCustomerResponse AddCustomer(IAddCustomerRequest request) { var isCustomerAdded = this.repository.AddCustomer(request.FirstName, request.SurName, request.Email, request.DateOfBirth, request.CompanyId); var response = new AddCustomerResponse(request) { IsCustomerAdded = isCustomerAdded }; return(response); }
public ResponseBase Update(UpdateCustomerRequest request) { if (ModelState.IsValid) { return(RequestManager.Execute(request)); } else { AddCustomerResponse responseObject = new AddCustomerResponse(); responseObject.Message = "Validation Failed"; responseObject.Errors = ModelState.Keys .SelectMany(key => ModelState[key].Errors.Select(x => new ValidationError(key, x.ErrorMessage))) .ToList(); return(responseObject); } }
public override ResponseBase ExecuteRequest() { AddCustomerResponse responseObject = new AddCustomerResponse(); try { using (CustomerDemoEntities ctx = new CustomerDemoEntities()) { var _customer = ctx.Customers.Where(c => c.ID == CustomerID).FirstOrDefault(); if (_customer != null) { if (_customer.PhoneNumbers.Count > 0) { var _phoneNumbers = ctx.PhoneNumbers.Where(p => p.CustomerID == CustomerID).ToList(); ctx.PhoneNumbers.RemoveRange(_phoneNumbers); } ctx.Customers.Remove(_customer); ctx.SaveChanges(); responseObject.Message = "Customer deleted Successufully"; responseObject.IsSuccess = true; } else { responseObject.IsSuccess = false; responseObject.Message = "Customer not exist"; } } } catch (Exception e) { responseObject.IsSuccess = false; responseObject.Message = e.Message; } return(responseObject); }