Example #1
0
        public async Task <IActionResult> Delete([FromBody] FlashObservationViewModel energyObservation)
        {
            _logger.LogInformation("Try to remove observation from database.");

            if (!ModelState.IsValid)
            {
                _logger.LogError($"Unable to remove observation with ID {energyObservation.Id} due to model state");

                return(BadRequest(ModelState));
            }

            try
            {
                await _service.DeleteObservationAsync(energyObservation);

                _logger.LogError($"Observation with id {energyObservation.Id} has been created successfully");

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Unable to remove observation with id {energyObservation.Id} due to: {ex.StackTrace}");

                return(BadRequest($"Cannot remove Flash Observation with id: {energyObservation.Id}"));
            }
        }
Example #2
0
        public async Task <IActionResult> Post([FromBody] FlashObservationViewModel energyObservation)
        {
            _logger.LogInformation("Try to post new observation to database.");

            if (!ModelState.IsValid)
            {
                _logger.LogError($"Unable to save observation with ID {energyObservation.Id} due to model state");

                return(BadRequest(ModelState));
            }

            try
            {
                await _service.AddObservationAsync(energyObservation);

                _logger.LogError($"Observation with id {energyObservation.Id} has been created successfully");

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Unable to save observation with id {energyObservation.Id} due to: {ex.StackTrace}");

                return(BadRequest("Cannot create new Flash Observation. See your request."));
            }
        }
Example #3
0
        public async Task DeleteObservationAsync(FlashObservationViewModel flashObservation)
        {
            if (_storage is BsonStorage <IEnergyObservation> bsonStorage)
            {
                var observartion = _mapper.Map <FlashObservation>(flashObservation);

                await Task.Run(() => bsonStorage.Remove(observartion));
            }
        }
Example #4
0
        public async Task AddObservationAsync(FlashObservationViewModel flashObservation)
        {
            if (_storage is BsonStorage <IEnergyObservation> bsonStorage)
            {
                var observartion = _mapper.Map <FlashObservation>(flashObservation);

                await Task.Run(() => bsonStorage.Add(observartion));

                _cache.Set(flashObservation.Id, flashObservation, new MemoryCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)
                });
            }
        }