public async Task <IActionResult> Post([FromBody] ContactAddClient contact) { try { var result = await _contactService.AddedAsync(contact); if (result.IsError) { result.StatuCode = 400; return(BadRequest(result)); } return(Ok(result)); } catch (Exception ex) { _logger.LogError(ex, "Error has encounterd"); return(StatusCode(500)); } }
public async Task <Result> AddedAsync(ContactAddClient contactClient) { var result = new Result(); await Task.Run(() => { var existed_title = _titleRepository.Exist(new Title() { Name = contactClient.Title }); var title = new Title() { Name = contactClient.Name }; Address new_address = contactClient.GetAddress(); var existed_address = _addressRepository .Find(new AddressExistSpecification(new_address).SpecExpression()) .FirstOrDefault(); Address new_deliveryAddress = contactClient.GetDeliveryAdress(); Address existed_deliveryAddress = null; if (!contactClient.IsDeliverSameAsHomeAddress) { existed_deliveryAddress = _addressRepository .Find(new AddressExistSpecification(new_deliveryAddress).SpecExpression()) .FirstOrDefault(); if (existed_deliveryAddress == null) { _addressRepository.Add(new_deliveryAddress); _addressRepository.Complete(); } } var contact = contactClient.GetContact(); contact.TitleId = existed_title == null ? title.Id : existed_title.Id; contact.Title = existed_title == null ? title : null; contact.HomeAddress = existed_address == null ? new_address : null; /* we don't want to add existed address again (fail) */ contact.AddressId = existed_address == null ? new_address.Id : existed_address.Id; contact.DeliveryAddressId = contactClient.IsDeliverSameAsHomeAddress ? new_address.Id : (existed_deliveryAddress != null ? existed_deliveryAddress.Id : new_deliveryAddress.Id); var existed_contact = _contactRepository.Find(new ContactExistSpecification(contact).SpecExpression()); if (existed_contact == null) { _contactRepository.Add(contact); } else { result.Message = "Client exist, no new contact is created"; result.IsError = true; } }); _contactRepository.Complete(); return(result); }
public static bool IsSameAddress(ContactAddClient contact, string v) { return(contact.IsDeliverSameAsHomeAddress); }