Beispiel #1
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 #2
0
        public async Task <GasLog> LogHistoryAsync(GasLog entity)
        {
            Guard.Against <ArgumentNullException>(entity == null, nameof(entity));

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

            if (!Validator.IsValidEntity(entity, ProcessResult))
            {
                return(null);
            }

            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);
            }
        }