public IActionResult PutAnnouncement(Guid id, [FromBody] AnnouncementForUpdate announcement) { if (announcement == null) { return(BadRequest()); } var announcementFromDb = _announcementsRepository.GetAnnouncement(id); if (announcementFromDb == null) { var announcementToCreate = Mapper.Map <Announcement>(announcement); announcementToCreate.ID = id; _announcementsRepository.AddAnnouncement(announcementToCreate); return(SaveAndGetAnnouncement(announcementToCreate, "Announcement for putting not found and for alternate, createing an announcement failed on save.")); } Mapper.Map(announcement, announcementFromDb); _announcementsRepository.UpdateAnnouncement(announcementFromDb); return(SaveAndGetAnnouncement(announcementFromDb, "Putting an announcement failed on save.")); }
public async Task <IActionResult> Edit(Guid id, AnnouncementForUpdate announcement) { if (announcement == null) { return(BadRequest()); } if (ModelState.IsValid) { var files = HttpContext.Request.Form.Files; if (files != null && files.Count > 0) { await announcement.UloadFilesIfExistsAsync(files, _hostingEnvironment); } else { var responseForImageName = await _announcementClient.GetAnnouncementAsync(id, "imagename"); var imageName = responseForImageName.Stream.GetDeserializedDataFromStream <AnnouncementGettingViewModel>().ImageName; announcement.ImageName = imageName; } var response = await _announcementClient.PutAnnouncementAsync(id, announcement); var updatedAnnouncement = response.Stream.GetDeserializedDataFromStream <AnnouncementGettingViewModel>(); return(RedirectToAction(nameof(Details), new { id = updatedAnnouncement.ID })); } return(View(announcement)); }
public IActionResult PatchAnnouncement(Guid id, [FromBody] JsonPatchDocument <AnnouncementForUpdate> patchDoc) { if (patchDoc == null) { return(BadRequest()); } var announcementFromDb = _announcementsRepository.GetAnnouncement(id); if (announcementFromDb == null) { var announcementForUpdate = new AnnouncementForUpdate(); patchDoc.ApplyTo(announcementForUpdate, ModelState); TryValidateModel(announcementForUpdate); if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var announcementToCreate = Mapper.Map <Announcement>(announcementForUpdate); announcementToCreate.ID = id; _announcementsRepository.AddAnnouncement(announcementToCreate); return(SaveAndGetAnnouncement(announcementToCreate, "Announcement for patching not found and for alternate, createing an announcement failed on save.")); } var announcementToPatch = Mapper.Map <AnnouncementForUpdate>(announcementFromDb); patchDoc.ApplyTo(announcementToPatch, ModelState); TryValidateModel(announcementToPatch); if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } Mapper.Map(announcementToPatch, announcementFromDb); _announcementsRepository.UpdateAnnouncement(announcementFromDb); return(SaveAndGetAnnouncement(announcementFromDb, "Patching an announcement failed on save.")); }