コード例 #1
0
ファイル: ContactsController.cs プロジェクト: MSH/PViMS-2
        public async Task <IActionResult> UpdateContactDetail(int id,
                                                              [FromBody] ContactForUpdateDto contactForUpdateDto)
        {
            if (contactForUpdateDto == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
            }

            var command = new ChangeContactDetailsCommand(id,
                                                          contactForUpdateDto.OrganisationType,
                                                          contactForUpdateDto.OrganisationName,
                                                          contactForUpdateDto.DepartmentName,
                                                          contactForUpdateDto.ContactFirstName,
                                                          contactForUpdateDto.ContactLastName,
                                                          contactForUpdateDto.StreetAddress,
                                                          contactForUpdateDto.City,
                                                          contactForUpdateDto.State,
                                                          contactForUpdateDto.PostCode,
                                                          contactForUpdateDto.CountryCode,
                                                          contactForUpdateDto.ContactNumber,
                                                          contactForUpdateDto.ContactEmail
                                                          );

            _logger.LogInformation($"----- Sending command: ChangeContactDetailsCommand - {command.Id}");

            var commandResult = await _mediator.Send(command);

            if (!commandResult)
            {
                return(BadRequest("Command not created"));
            }

            return(Ok());
        }
コード例 #2
0
ファイル: ContactController.cs プロジェクト: iretibe/PAB.API
        public async Task <IActionResult> UpdateContact(Guid contactId, [FromBody] ContactForUpdateDto contact)
        {
            if (contact == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            //var userId = contact.UserId;

            //get logged in user id from claims
            contact.UserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "sub")?.Value);

            var contactFromRepo = await _contactNameRepo.GetContactById(contactId, contact.UserId);

            if (contactFromRepo == null)
            {
                return(NotFound());
            }

            Mapper.Map(contact, contactFromRepo);

            _contactNameRepo.Update(contactFromRepo);

            if (await _contactNameRepo.Save() == false)
            {
                throw new Exception($"Updating contact with Id: {contactId} failed on save.");
            }

            return(NoContent());
        }
コード例 #3
0
        public ContactTest()
        {
            _contactService = new DataService <Contact>();
            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutoMapperProfiles());
            });

            _mapper = mockMapper.CreateMapper();



            _contactForAddDto = new ContactForAddDto
            {
                Name    = "Test Contact",
                Address = "1 See Stree MedowBank College",
                Details = "This is the testing details for ContactDto"
            };
            _contactForUpdateDto = new ContactForUpdateDto
            {
                Name    = "Updating The Contact for testing",
                Address = "Updated Address 1 See Stree MedowBank College",
                Details = "This is the testing checking for updating the details",
            };
        }
コード例 #4
0
        public ActionResult PatchContact(Guid companyId, Guid contactId, [FromBody] JsonPatchDocument <ContactForUpdateDto> patchDocument)
        {
            if (contactId == null)
            {
                return(BadRequest());
            }

            if (patchDocument == null)
            {
                return(BadRequest());
            }

            if (!_contactRepository.ContactExists(contactId))
            {
                return(NotFound());
            }

            Contact contactFromRepo = _contactRepository.GetCompanyContact(companyId, contactId);

            if (contactFromRepo == null)
            {
                var contactDto = new ContactForUpdateDto();
                patchDocument.ApplyTo(contactDto, ModelState);

                if (!TryValidateModel(contactDto))
                {
                    return(ValidationProblem(ModelState));
                }

                var contactToAdd = _mapper.Map <Entities.Contact>(contactDto);
                contactToAdd.Id = contactId;

                _contactRepository.CreateCompanyContact(companyId, contactToAdd);

                _contactRepository.Save();

                var contactToReturn = _mapper.Map <ContactDto>(contactToAdd);

                return(CreatedAtRoute("GetContact",
                                      new { contactId }, contactToReturn));
            }

            var contactToPatch = _mapper.Map <ContactForUpdateDto>(contactFromRepo);

            patchDocument.ApplyTo(contactToPatch, ModelState);;

            if (!TryValidateModel(contactToPatch))
            {
                return(ValidationProblem(ModelState));
            }

            _mapper.Map(contactToPatch, contactFromRepo);

            _contactRepository.PatchContact(contactFromRepo);

            _contactRepository.Save();

            return(NoContent());
        }
コード例 #5
0
        public async Task <IActionResult> UpdateContact(int id, ContactForUpdateDto ContactForUpdateDto)
        {
            var contactFromRepo = await _repo.GetContact(id);

            _mapper.Map(ContactForUpdateDto, contactFromRepo);
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception($"Updating contact with {id} failed on save");
        }
コード例 #6
0
        public async Task <IActionResult> CreateContact(ContactForUpdateDto newContact)
        {
            Contact contact = new Contact();

            _mapper.Map(newContact, contact);
            _repo.Add(contact);
            await _repo.SaveAll();

            int id = contact.Id;

            return(Ok(id));
        }
コード例 #7
0
        public ActionResult UpdateContact(int contactID, [FromBody] ContactForUpdateDto contactforUpdateDto)
        {
            var contactFromRepo = this.contactRepository.GetContact(contactID);

            if (contactFromRepo == null)
            {
                return(NotFound("Contact with ID-" + contactID + " did not exist!"));
            }

            this.mapper.Map(contactforUpdateDto, contactFromRepo);
            this.contactRepository.UpdateContact(contactFromRepo);
            return(NoContent());
        }
コード例 #8
0
        public async Task <IActionResult> UpdateContact(int id, ContactForUpdateDto contactForUpdateDto)
        {
            Contact contact = await _contactService.GetContact(id);

            _mapper.Map(contactForUpdateDto, contact);


            if (await _contactService.Update(contact))
            {
                var contactView = _mapper.Map <ContactForViewDto>(contact);
                return(Ok(contactView));
            }

            throw new Exception($"Updating project {id} failed on save");
        }
コード例 #9
0
        public async Task <IActionResult> UpdateContact(int id, ContactForUpdateDto contactForUpdateDto)
        {
            if (!await _contactRepository.ContactIdExist(id))
            {
                return(BadRequest("Unknown Contact"));
            }

            var contactFromRepo = await _contactRepository.GetContact(id);

            _mapper.Map(contactForUpdateDto, contactFromRepo);

            if (await _contactRepository.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating user {id} failed on save");
        }
コード例 #10
0
        public async Task <IActionResult> UpdateContact(int contactId, [FromBody] ContactForUpdateDto input)
        {
            if (ModelState.IsValid)
            {
                var contactInDB = await _contactService.GetContactById(contactId);

                if (contactInDB == null)
                {
                    return(NotFound(contactId));
                }
                var result = await _contactService.UpdateContact(_mapper.Map(input, contactInDB));

                if (result)
                {
                    return(Ok());
                }
            }
            return(BadRequest(new { message = ModelState.Values.First().Errors[0].ErrorMessage }));
        }