/// <summary> /// Adds a complaint to an existing feed /// </summary> /// <param name="feedId"></param> /// <param name="complaint"></param> /// <returns></returns> public async Task <Response <ComplaintResponseDTO> > AddComplaint(string feedId, AddComplaintDTO complaint) { var response = new Response <ComplaintResponseDTO>(); var feed = await _feedRepo.GetById(feedId); //ends the process if the feed cannot be found if (feed == null) { response.Message = "Feed not found"; response.Success = false; return(response); } //map values of the DTO to the actual object var newComplaint = ComplaintMapper.FromAddComplaintDTO(complaint); //updates the feedId in the categoryId property newComplaint.CategoryId = feedId; var result = await _complaintsRepo.Add(newComplaint); response.Success = result; response.Message = !response.Success ? "Error occured while updating your entry" : "Updated successfully"; response.Data = ComplaintMapper.ToComplaintResponseDTO(newComplaint); //returns the complaint added and the related field return(response); }
public async Task <IActionResult> PostAsync([FromBody] ComplaintDTO value) { var now = DateTime.Now; try { var fillTask = FillComplaint(value); var existingTask = ComplaintRepository.Find(value.SubmittedDate, value.ComplaintDetails); var complaint = await fillTask; var existing = await existingTask; complaint.Key = existing?.Key ?? 0; complaint.FirstSeen = existing != null && existing.FirstSeen != DateTime.MinValue ? existing.FirstSeen : now; complaint.LastSeen = now; var upsertTask = existing == null?ComplaintRepository.Add(complaint) : ComplaintRepository.Update(complaint); return(Ok(Mapper.Map <ComplaintDTO>(await upsertTask))); } catch (Exception e) { Logger.LogError("Error adding complaint:\n{message}", e.Message); return(BadRequest(e.ToString())); } }