public async Task <TPAnnouncementDto> CreateAsync(TPAnnouncementDto announcementDto, Guid loggedInUserId) { try { var tpToAnnounceTo = await _travelPlanService.GetAsync(announcementDto.TravelPlanId); if (tpToAnnounceTo.CreatedById != loggedInUserId) { throw new InsufficientRightsException("User doesn't have rights to delete"); } var newAnnouncement = new TPAnnouncement { Title = announcementDto.Title, Description = announcementDto.Description, CreatedDate = DateTime.UtcNow, CreatedById = loggedInUserId, TravelPlanId = announcementDto.TravelPlanId, TravelPlanActivityId = announcementDto.TravelPlanActivityId }; var addedAnnouncement = await _announcementRepository.CreateAsync(newAnnouncement); return(new TPAnnouncementDto(addedAnnouncement)); } catch (Exception) { throw; } }
public async Task <IActionResult> Edit([FromBody] TPAnnouncementDto announcementDto) { try { var userId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value; var announcement = await _announcementRepo.EditAsync(announcementDto, new Guid(userId)); return(Ok(announcement)); } catch (Exception) { throw; } }
public async Task <TPAnnouncementDto> EditAsync(TPAnnouncementDto announcementDto, Guid loggedInUserId) { try { //validate announcement exists var announcementToEdit = await _dbContext.TPAnnouncements.FindAsync(announcementDto.Id); if (announcementToEdit == null) { throw new CommonException("Invalid Announcement"); } //validate logged in user is even able to make an announcement to travel plan var tpToAnnounceTo = await _travelPlanRepository.GetAsync(announcementDto.TravelPlanId); if (tpToAnnounceTo.CreatedById != loggedInUserId) { throw new InsufficientRightsException("User doesn't have rights to delete"); } announcementToEdit.Title = announcementDto.Title; announcementToEdit.Description = announcementDto.Description; announcementToEdit.CreatedDate = announcementDto.CreatedDate; //probably create a new column for updated date if (!_dbContext.ChangeTracker.HasChanges()) { return(announcementDto); } var isSuccessful = await _dbContext.SaveChangesAsync() > 0; if (isSuccessful) { return(new TPAnnouncementDto(announcementToEdit)); } throw new CommonException("Problem occurred creating announcement"); } catch (Exception) { throw; } }
public async Task <IActionResult> Edit([FromBody] TPAnnouncementDto announcementDto) { try { var userId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value; var announcement = await _announcementService.EditAsync(announcementDto, new Guid(userId)); return(Ok(announcement)); } catch (InsufficientRightsException insufExc) { return(BadRequest(new { Message = insufExc.Message })); } catch (CommonException commExc) { return(BadRequest(new { Message = commExc.Message })); } catch (Exception) { return(BadRequest(new { Message = "Error Occurred" })); } }
public async Task <TPAnnouncementDto> CreateAsync(TPAnnouncementDto announcementDto, Guid loggedInUserId) { try { //validate logged in user is even able to make an announcement to travel plan var tpToAnnounceTo = await _travelPlanRepository.GetAsync(announcementDto.TravelPlanId); if (tpToAnnounceTo.CreatedById != loggedInUserId) { throw new InsufficientRightsException("User doesn't have rights to delete"); } var newAnnouncement = new TPAnnouncement { Title = announcementDto.Title, Description = announcementDto.Description, CreatedDate = DateTime.UtcNow, CreatedById = loggedInUserId, TravelPlanId = announcementDto.TravelPlanId, TravelPlanActivityId = announcementDto.TravelPlanActivityId }; _dbContext.TPAnnouncements.Add(newAnnouncement); var isSuccessful = await _dbContext.SaveChangesAsync() > 0; if (isSuccessful) { return(announcementDto); } throw new CommonException("Problem occurred creating announcement"); } catch (Exception) { throw; } }