public IActionResult Update(string id, [FromBody] VolunteerDetails volunteerDetailsIn) { id = id.ToLower(); VolunteersResponse.Success = false; if (volunteerDetailsIn == null) { VolunteersResponse.Message = "volunteer submitted was null."; return(BadRequest(new[] { VolunteersResponse })); } if (!ModelState.IsValid) { VolunteersResponse.Message = $"Not all fields were supplied! {ModelState}"; return(BadRequest(new[] { VolunteersResponse })); } try { var existing = _volunteersService.Get(id); if (existing == null) { VolunteersResponse.Message = "volunteer record not found"; return(NotFound(new[] { VolunteersResponse })); } var updated = _volunteersService.Update(id, volunteerDetailsIn); var volunteerFound = new List <Volunteer>() { _volunteersService.Get(id) }; VolunteersResponse.Data = volunteerFound; if (!updated) { VolunteersResponse.NumberOfRecordsFound = volunteerFound.Count; VolunteersResponse.Message = "Update didn't work!"; return(BadRequest(new[] { VolunteersResponse })); } VolunteersResponse.Success = true; VolunteersResponse.NumberOfRecordsFound = volunteerFound.Count; VolunteersResponse.Message = "volunteer record updated"; return(Ok(new[] { VolunteersResponse })); } catch (MongoException ex) { _logger.LogDebug($"Error updating volunteer record in the DB: {ex.Message}"); } catch (Exception ex) { _logger.LogDebug($"Error: {ex.Message}"); } _logger.LogInformation("Error: update request cannot be handled, bad request."); return(BadRequest()); }
public bool Update(string id, VolunteerDetails volunteerIn) { _logger.LogDebug($"id: {id}"); var filter = Builders <Volunteer> .Filter.Eq(s => s.Id, id); var update = Builders <Volunteer> .Update .Set(volunteer => volunteer.Details, volunteerIn) .CurrentDate(s => s.UpdatedAt); try { var v = _volunteers.UpdateOne(filter, update); var volunteerdIdUpdated = v.UpsertedId; return(true); } catch (Exception ex) { _logger.LogDebug(ex.Message); } return(false); }