Beispiel #1
0
        public async Task <IActionResult> GetAutomobileById(int id)
        {
            var car = await _automobileManager.GetEntityByIdAsync(id);

            if (car == null)
            {
                return(NotFound());
            }

            return(Ok(car));
        }
Beispiel #2
0
        public async Task <IActionResult> Get(int id)
        {
            var discount = await _discountManager.GetEntityByIdAsync(id);

            if (discount == null)
            {
                return(NotFound());
            }

            return(Ok(discount));
        }
Beispiel #3
0
        public override async Task <GasLog> CreateAsync(GasLog entity)
        {
            Guard.Against <ArgumentNullException>(entity == null, nameof(entity));

            // ReSharper disable once PossibleNullReferenceException
            entity.AuthorId   = DefaultAuthor.Id;
            entity.CreateDate = _dateProvider.UtcNow;

            // ToDo: validate current meter reader with auto meter reader
            if (!Validator.IsValidEntity(entity, ProcessResult))
            {
                return(null);
            }

            var auto = await _autoManager.GetEntityByIdAsync(entity.Car.Id);

            if (auto == null)
            {
                ProcessResult.AddErrorMessage("Cannot find automobile information");
                return(null);
            }

            var(isMeterValid, validationError) = MeterReadingValid(entity, auto);
            if (!isMeterValid)
            {
                ProcessResult.AddErrorMessage(validationError);
                return(null);
            }

            if (!string.IsNullOrEmpty(validationError))
            {
                ProcessResult.AddWaningMessage(validationError);
            }

            // Update automobile meter reader
            auto.MeterReading = (long)entity.CurrentMeterReading.TotalKilometre;
            var updatedAuto = await _autoManager.UpdateAsync(auto);

            if (updatedAuto == null)
            {
                ProcessResult.PropagandaResult(_autoManager.ProcessResult);
                return(null);
            }

            // ReSharper disable once PossibleNullReferenceException
            var note = entity.GetNote(NoteSerializer, DefaultAuthor);
            await NoteManager.CreateAsync(note);

            switch (NoteManager.ProcessResult.Success)
            {
            case false:
                ProcessResult.PropagandaResult(NoteManager.ProcessResult);
                return(null);

            default:
                var newLog = await GetEntityByIdAsync(note.Id);

                return(newLog);
            }
        }
Beispiel #4
0
        public async Task <ActionResult> HistoryLog(int autoId, [FromBody] ApiGasLogForCreation apiGasLog)
        {
            if (apiGasLog == null)
            {
                var errMsg = "null gas log found";
                Log.Logger.Debug(errMsg);
                return(BadRequest(new ApiBadRequestResponse(errMsg)));
            }

            try
            {
                var gasLog = _mapper.Map <GasLog>(apiGasLog);

                // get automobile
                var car = await _autoManager.GetEntityByIdAsync(autoId);

                if (car == null)
                {
                    var errMsg = $"Cannot find automobile with id {apiGasLog.AutomobileId} from data source";
                    Log.Logger.Debug(errMsg);
                    return(BadRequest(new ApiBadRequestResponse(errMsg)));
                }

                gasLog.Car = car;

                // get discount for gas log
                var discounts = new List <GasDiscountInfo>();
                if (apiGasLog.DiscountInfos != null)
                {
                    foreach (var disc in apiGasLog.DiscountInfos)
                    {
                        var discount = await _discountManager.GetEntityByIdAsync(disc.DiscountId);

                        if (discount == null)
                        {
                            var errMsg =
                                $"Cannot find discount information for discount with id {disc.DiscountId} from data source";
                            return(BadRequest(new ApiBadRequestResponse(errMsg)));
                        }

                        discounts.Add(new GasDiscountInfo
                        {
                            Program = discount,
                            Amount  = new Money(disc.Amount)
                        });
                    }
                }

                gasLog.Discounts = discounts;

                var savedGasLog = await _gasLogManager.LogHistoryAsync(gasLog);

                if (savedGasLog == null)
                {
                    var errMsg = _gasLogManager.ProcessResult.GetWholeMessage();
                    return(BadRequest(new ApiBadRequestResponse(errMsg)));
                }
                return(Ok(savedGasLog));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }