public Contact Map(ContactApiModel contactApiModel) { var contact = new Contact(); Map(contactApiModel, contact); return(contact); }
public IActionResult Update(int id, [FromBody] ContactApiModel contact) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { Contact contactDb = _contactRepository.GetSingle(id); if (contactDb == null) { return(NotFound("Contact having Id - " + id + " not found")); } else { contactDb.FirstName = contact.FirstName; contactDb.LastName = contact.LastName; contactDb.Email = contact.Email; contactDb.PhoneNumber = contact.PhoneNumber; contactDb.Status = contact.Status; _contactRepository.Update(contactDb); _contactRepository.Commit(); return(new OkObjectResult("Contact having Id - " + id + " updated successfully.")); } } catch { throw new Exception("Error occcured while processing your request."); } }
public void Map(ContactApiModel contactApiModel, Contact contact) { contact.FirstName = contactApiModel.FirstName; contact.LastName = contactApiModel.LastName; contact.Email = contactApiModel.Email; contact.MobilePhone = contactApiModel.MobilePhone; }
public ActionResult Update(Guid id, ContactApiModel contactApiModel) { try { ApiResponse serviceResponse = this._contactService.GetById(id); if (serviceResponse.IsSuccess() == false) { return(new ObjectNotFoundResult(serviceResponse)); } Contact contact = serviceResponse.GetData <Contact>(); this._contactMapper.Map(contactApiModel, contact); serviceResponse = this._contactService.Update(id, contact); if (serviceResponse.IsSuccess()) { return(new ObjectUpdatedResult(serviceResponse.Id.Value)); } { return(new ValidationErrorResult(serviceResponse)); } } catch (Exception ex) { return(new UnknownErrorResult(ex, base._errorEnabled)); } }
private void SimulateValidation(ContactApiModel model) { var validationContext = new ValidationContext(model, null, null); var validationResults = new List <ValidationResult>(); Validator.TryValidateObject(model, validationContext, validationResults, true); foreach (var validationResult in validationResults) { _controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage); } }
public ContactApiModel Map(Contact contact) { ContactApiModel contactApiModel = new ContactApiModel(); contactApiModel.Id = contact.Id; contactApiModel.FirstName = contact.FirstName; contactApiModel.LastName = contact.LastName; contactApiModel.Email = contact.Email; contactApiModel.MobilePhone = contact.MobilePhone; return(contactApiModel); }
private void SimulateValidation(ContactApiModel model) { // mimic the behaviour of the model binder which is responsible for Validating the Model var validationContext = new ValidationContext(model, null, null); var validationResults = new List <ValidationResult>(); Validator.TryValidateObject(model, validationContext, validationResults, true); foreach (var validationResult in validationResults) { _controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage); } }
public ActionResult Add(Guid communityId, ContactApiModel contactApiModel) { try { var contact = _contactMapper.Map(contactApiModel); var response = _contactService.Add(communityId, contact); return(Ok(response)); } catch (Exception ex) { return(new UnknownErrorResult(ex, base._errorEnabled)); } }
public CreateContactMsgEntity MapCreateContactMsgEntity(ContactApiModel contactApiModel) { if (contactApiModel == null) { return(new CreateContactMsgEntity()); } return(new CreateContactMsgEntity() { CustomerName = contactApiModel.CustomerName, EmailAddress = contactApiModel.EmailAddress, Message = contactApiModel.Message, Subject = contactApiModel.Subject }); }
public static TModel FromVendor <TModel>(Vendor vendor) where TModel : VendorApiModel, new() { var model = new TModel(); model.Id = vendor.Id; model.TenantId = vendor.TenantId; model.Name = vendor.Name; model.VendorDocuments = vendor.VendorDocuments.Select(x => VendorDocumentApiModel.FromVendorDocument(x)).ToList(); model.VendorSelectionCriterion = vendor.VendorSelectionCriterion.Select(x => VendorSelectionCriteriaApiModel.FromVendorSelectionCriteria(x)).ToList(); model.Contacts = vendor.Contacts.Select(x => ContactApiModel.FromContact(x)).ToList(); return(model); }
public void Create_ReturnsBadRequest_GivenInvalidModelData() { var contactApiModel = new ContactApiModel(); contactApiModel.Email = string.Empty; contactApiModel.FirstName = "TestName"; contactApiModel.LastName = "TestLastName"; contactApiModel.PhoneNumber = "9011534816"; contactApiModel.Status = Lib.Model.ContactStatus.Active; SimulateValidation(contactApiModel); var result = _controller.Create(contactApiModel); Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); }
public ActionResult Post(ContactApiModel contactApiModel) { try { var createContactMsgEntity = contactServiceControllerMapper.MapCreateContactMsgEntity(contactApiModel); var errorMessages = contactService.CreateContact(createContactMsgEntity); var contactApiResponseModel = contactServiceControllerMapper.MapContactApiResponseModel(errorMessages, createContactMsgEntity); if (contactApiResponseModel.ErrorMessages.IsEmpty()) { return(Ok(contactApiResponseModel)); } return(StatusCode(400, contactApiResponseModel)); } catch (Exception ex) { //TODO: log error return(StatusCode(500, contactServiceErrorCode.InternalError)); } }
public IActionResult Create([FromBody] ContactApiModel contactApiModel) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Contact contact = ContactApiMapper.MapToEntiry(contactApiModel); _contactRepository.Add(contact); _contactRepository.Commit(); CreatedAtRouteResult result = CreatedAtRoute("GetContact", new { controller = "Contacts", id = contact.Id }, contact); return(result); } catch { throw new Exception("Error occcured while processing your request."); } }
public void Create_GivenValidModel() { var mockRepo = new Mock <IContactRepository>(); var controller = new ContactsController(mockRepo.Object); var contactApiModel = new ContactApiModel(); contactApiModel.Email = "*****@*****.**"; contactApiModel.FirstName = "TestName"; contactApiModel.LastName = "TestLastName"; contactApiModel.PhoneNumber = "9011534816"; contactApiModel.Status = Lib.Model.ContactStatus.Active; CreatedAtRouteResult result = (CreatedAtRouteResult)controller.Create(contactApiModel); Assert.IsInstanceOfType(result, typeof(CreatedAtRouteResult)); Contact contact = (Contact)result.Value; Assert.AreEqual("*****@*****.**", contact.Email); Assert.AreEqual("TestName", contact.FirstName); Assert.AreEqual("TestLastName", contact.LastName); Assert.AreEqual("9011534816", contact.PhoneNumber); Assert.AreEqual(ContactStatus.Active, contact.Status); }
public async Task <IActionResult> GetContact([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var contact = await _context.Contacts .Include(n => n.Numbers) .Include(e => e.Emails) .Include(ct => ct.ContactTags) .SingleOrDefaultAsync(m => m.Id == id); if (contact == null) { return(NotFound()); } var tags = new List <TagApiModel>(); foreach (var tagId in contact.ContactTags.Select(t => t.TagId)) { foreach (var tag in _context.Tags.Where(t => t.Id == tagId)) { tags.Add(new TagApiModel { Id = tag.Id, Name = tag.Name }); } } var numbers = new List <PhoneNumberApiModel>(); foreach (var number in contact.Numbers) { numbers.Add(new PhoneNumberApiModel { Id = number.Id, Number = number.Number, PhoneTypeId = number.PhoneTypeId, Type = _context.PhoneTypes.SingleOrDefault(t => t.Id == number.PhoneTypeId).Type }); } var emails = new List <EmailApiModel>(); foreach (var email in contact.Emails) { emails.Add(new EmailApiModel { Id = email.Id, EmailAddress = email.EmailAddress }); } var contactToReturn = new ContactApiModel { Id = contact.Id, FirstName = contact.FirstName, LastName = contact.LastName, Address = contact.Address, Emails = emails, Tags = tags, Numbers = numbers }; return(Ok(contactToReturn)); }