public async Task <IActionResult> HealthRequestReply([FromBody] HealthRequestReply model) { //Check if the model is valid if (ModelState.IsValid) { //Set the user id model.UserId = Guid.Parse(User.GetClaimValue(ClaimTypes.NameIdentifier)); //Try to add of update the blog var reply = await HealthRequestService.AddOrUpdateRequestReply(model); //Check if the operation was not succesfull if (reply != null) { return(Ok(new { reply.Content, reply.Id, CreationDate = reply.CreationDate.ToString(DateFormats.DefaultDate), reply.RequestId, })); } return(InternalServerError()); } return(CustomBadRequest(ModelState.GetValidationErrors())); }
public async Task <HealthRequestReply> AddOrUpdateRequestReply(HealthRequestReply healthRequestReply) { try { //If there is no id assigned to the reply if (healthRequestReply.Id == Guid.Empty) { //Then we need to add a new record DbContext.HealthRequestReplies.Add(healthRequestReply); //Get the request var healthRequest = await DbContext.HealthRequests.SingleOrDefaultAsync(item => item.Id == healthRequestReply.RequestId); healthRequest.ReplyCount++; DbContext.Entry(healthRequest).State = EntityState.Modified; } else { //Get the blokg from the db var reply = await DbContext.HealthRequestReplies.SingleOrDefaultAsync(item => item.Id == healthRequestReply.Id); //Update the wanted values reply.Content = healthRequestReply.Content; reply.Note = healthRequestReply.Note; healthRequestReply = reply; //Mark it as modified DbContext.Entry(reply).State = EntityState.Modified; } await DbContext.SaveChangesAsync(); return(healthRequestReply); } catch (System.Exception ex) { LogginService.LogException(ex); } return(null); }