Exemple #1
0
        public bool Update(string id, OpportunityDetails opportunityDetailsIn)
        {
            var filter = Builders <Opportunity> .Filter.Eq(o => o.Id, id);

            var update = Builders <Opportunity> .Update
                         .Set(opportunity => opportunity.Details, opportunityDetailsIn)
                         .CurrentDate(o => o.UpdatedAt);

            try
            {
                _logger.LogInformation($"Trying to update opportunity {id}");
                var opportunity          = _opportunities.UpdateOne(filter, update);
                var opportunityUpdatedId = opportunity.UpsertedId;
                if (opportunity.ModifiedCount == 1)
                {
                    _logger.LogInformation($"Updated successfully");
                    return(true);
                }
                throw new Exception("Update request completed but modified count does not equal 1.");
            }
            catch (MongoException ex)
            {
                _logger.LogError($"Error trying to update the record with id {id}: {ex.Message}");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error: {ex.Message}");
            }
            _logger.LogInformation($"Did not update record {id}");
            throw new Exception("Error. Something went wrong and record was not updated.");
        }
        public IActionResult Update(string id, [FromBody] OpportunityDetails opportunityDetailsIn)
        {
            id = id.ToLower();

            OpportunityResponse.Success = false;

            if (opportunityDetailsIn == null)
            {
                OpportunityResponse.Message = "Opportunity submitted was null";
                return(BadRequest(new[] { OpportunityResponse }));
            }

            try
            {
                var existing = _opportunitiesService.Get(id);

                if (existing == null)
                {
                    OpportunityResponse.Message = "Opportunity record not found";
                    return(NotFound(new[] { OpportunityResponse }));
                }

                var updated          = _opportunitiesService.Update(id, opportunityDetailsIn);
                var opportunityFound = new List <Opportunity>()
                {
                    _opportunitiesService.Get(id)
                };

                OpportunityResponse.Data = opportunityFound;
                if (!updated)
                {
                    OpportunityResponse.NumberOfRecordsFound = opportunityFound.Count;
                    OpportunityResponse.Message = "Update didn't work!";
                    return(BadRequest(new[] { OpportunityResponse }));
                }

                OpportunityResponse.Success = true;
                OpportunityResponse.NumberOfRecordsFound = opportunityFound.Count;
                OpportunityResponse.Message = "Opportunity record updated";
                return(Ok(new[] { OpportunityResponse }));
            }

            catch (MongoException ex)
            {
                _logger.LogError($"Error updating opportunity record in the DB: {ex.Message}");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error: {ex.Message}");
            }
            _logger.LogError("Error: update request cannot be handled, bad request");
            return(BadRequest());
        }