public Domain.MashResident UpdateMashResident(UpdateMashResidentRequest request, long mashResidentId) { var mashResident = _databaseContext.MashResidents .Include(x => x.MashReferral) .FirstOrDefault(x => x.Id == mashResidentId); if (mashResident == null) { throw new MashResidentNotFoundException($"MASH resident with id {mashResidentId} not found"); } if (request.UpdateType == "UNLINK-PERSON") { mashResident.SocialCareId = null; _databaseContext.SaveChanges(); return(mashResident.ToDomain()); } var person = _databaseContext.Persons.FirstOrDefault(x => x.Id == request.SocialCareId); if (person == null) { throw new PersonNotFoundException($"Person with id {request.SocialCareId} not found"); } mashResident.SocialCareId = person.Id; _databaseContext.SaveChanges(); return(mashResident.ToDomain()); }
public IActionResult UpdateMashResident([FromBody] UpdateMashResidentRequest request, long mashResidentId) { try { var updatedMashResident = _mashResidentUseCase.UpdateMashResident(request, mashResidentId); return(Ok(updatedMashResident)); } catch (Exception e) when( e is MashResidentNotFoundException || e is PersonNotFoundException) { return(BadRequest(e.Message)); } }
public async Task SuccessfulUnLinkingRemovesLinkFromMashResidentToSavedPersonInDb() { _linkedResident.SocialCareId.Should().NotBeNull(); var request = new UpdateMashResidentRequest { UpdateType = "UNLINK-PERSON" }; var patchUri = new Uri($"/api/v1/mash-resident/{_linkedResident.Id}", UriKind.Relative); var serializedRequest = JsonSerializer.Serialize(request); var requestContent = new StringContent(serializedRequest, Encoding.UTF8, "application/json"); var unlinkMashResidentResponse = await Client.PatchAsync(patchUri, requestContent).ConfigureAwait(true); unlinkMashResidentResponse.StatusCode.Should().Be(200); _linkedResident.SocialCareId.Should().BeNull(); }
public async Task SuccessfulLinkingMatchesMashResidentToSavedPersonInDb() { _unlinkedResident.SocialCareId = null; DatabaseContext.SaveChanges(); var request = new UpdateMashResidentRequest { SocialCareId = _existingDbPerson.Id }; var patchUri = new Uri($"/api/v1/mash-resident/{_unlinkedResident.Id}", UriKind.Relative); var serializedRequest = JsonSerializer.Serialize(request); var requestContent = new StringContent(serializedRequest, Encoding.UTF8, "application/json"); var linkMashResidentResponse = await Client.PatchAsync(patchUri, requestContent).ConfigureAwait(true); linkMashResidentResponse.StatusCode.Should().Be(200); _unlinkedResident.SocialCareId.Should().Be(_existingDbPerson.Id); var content = await linkMashResidentResponse.Content.ReadAsStringAsync().ConfigureAwait(true); var patchMashResidentResponse = JsonConvert.DeserializeObject <MashResidentResponse>(content); patchMashResidentResponse.SocialCareId.Should().Be(_existingDbPerson.Id); }
public MashResidentResponse UpdateMashResident(UpdateMashResidentRequest request, long mashResidentId) { var response = _mashReferralGateway.UpdateMashResident(request, mashResidentId); return(response.ToResponse()); }