public async Task <BaseResponse <ContactsDTO> > AddNewContact(ContactsDTO contact) { var validator = new ContactsDTOValidator(); var result = validator.Validate(contact); if (!result.IsValid) { return(new BaseResponse <ContactsDTO>() { ErrorStatus = ErrorStatus.Error, ErrorInfo = new ErrorInfo() { ErrorMessage = result.Errors.First().ErrorMessage } }); } _context.Contacts.Add(contact); await _context.SaveChangesAsync(); return(new BaseResponse <ContactsDTO>() { ErrorStatus = ErrorStatus.Success, Result = contact }); }
public async Task <BaseResponse <ContactsDTO> > UpdateContactAsync(int id, ContactsDTO contact) { var validator = new ContactsDTOValidator(); var result = validator.Validate(contact); if (!result.IsValid) { return(new BaseResponse <ContactsDTO>() { ErrorStatus = ErrorStatus.Error, ErrorInfo = new ErrorInfo() { ErrorMessage = result.Errors.First().ErrorMessage } }); } var contactData = await _context.Contacts.FirstOrDefaultAsync(i => i.ContactId == id); if (contactData != null) { contactData.DateUpdated = DateTime.Now; contactData.LastName = contact.LastName; contactData.Telephone = contact.Telephone; contactData.Mobile = contact.Mobile; contactData.Email = contact.Email; _context.Contacts.Attach(contactData); _context.Entry(contactData).State = EntityState.Modified; await _context.SaveChangesAsync(); return(new BaseResponse <ContactsDTO>() { ErrorStatus = ErrorStatus.Success, Result = contact }); } else { return(new BaseResponse <ContactsDTO>() { ErrorStatus = ErrorStatus.Failed, ErrorInfo = new ErrorInfo() { ErrorMessage = "Contact Not Found" }, Result = null }); } }