public async Task <EntityResponse <ContactDto> > CreateContactAsync(AddContactDto newContactDto) { var result = ModelValidator.ValidateAsEntityValidation(newContactDto, "E0001").As <ContactDto>(); if (!result.Ok) { return(result); } bool alreadyExists = _contactRepository.AsQueryable().Any(e => e.Email == newContactDto.Email); if (alreadyExists) { return(EntityResponse.CreateError("The email already exists.", "E0004").As <ContactDto>()); } Contact contact = newContactDto.CreateEntity(); await _contactRepository.AddAsync(contact); await unitOfWork.SaveChangesAsync(); ContactDto dto = new ContactDto { Email = contact.Email, Id = contact.Id, Name = contact.Name }; return(EntityResponse.CreateOk().As(dto)); }
public async Task <ActionResult <ContactDto> > CreateAsync([FromBody] AddContactDto contactDto) { var result = await _contactsAppService.CreateContactAsync(contactDto); if (!result.Ok) { return(BadRequest(result)); } return(result.Data); }
public async Task <IActionResult> CreateContact([FromBody] AddContactDto addContactDto, CancellationToken ct) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await _contactsService.AddContact(addContactDto, ct); return(Ok()); }
public async Task AddContact(AddContactDto addContactDto, CancellationToken ct) { var newContact = new Contact(addContactDto); try { _dbContext.Add(newContact); await _dbContext.SaveChangesAsync(ct); } catch (Exception ex) { _logger.LogError($"{DateTime.Now:g}: {ex.Message}"); throw; } }
public Contact(AddContactDto addDto) { FirstName = addDto.FirstName; LastName = addDto.LastName; MiddleName = addDto.MiddleName; if (addDto.BirthDate.HasValue) { var birthDate = addDto.BirthDate.Value; BirthDate = new DateTime(birthDate.Year, birthDate.Month, birthDate.Day); } OrganizationName = addDto.OrganizationName; OrganizationPost = addDto.OrganizationPost; ContactInfos = addDto.ContactInfos .Select(x => new ContactInfo { Value = x.Value, Type = x.Type }) .ToList(); }
public void Contacts_ValidateContactInformation() { //Arrange var unitOfWork = new Mock <IUnitOfWork>(); var repository = new Mock <IRepository <Contact> >(); unitOfWork.Setup(e => e.GetRepository <Contact>()).Returns(repository.Object); var appService = new ContactsAppService(unitOfWork.Object, null); var createDto = new AddContactDto(); //Act var result = appService.CreateContactAsync(createDto).GetAwaiter().GetResult(); createDto.Name = "Omar"; var result2 = appService.CreateContactAsync(createDto).GetAwaiter().GetResult(); //Assert Assert.AreEqual(expected: false, result.Ok); Assert.AreEqual(expected: "E0001", result.MessageCode); Assert.AreEqual(expected: "The name is required", result.Message); Assert.AreEqual(expected: "The email is required", result2.Message); }