Example #1
0
        private async Task <IActionResult> UpdateStates(int IdCar)
        {
            AccuListWithTime accu = await carCheckingService.GetAccuState(IdCar);

            if (accu == null)
            {
                return(NotFound());
            }
            logger.LogDebug($"Start saving time code");
            TimeCodeHistory timeCodeHistory = new TimeCodeHistory(accu.idCar, accu.time);

            _context.TimeCodes.Add(timeCodeHistory);
            await _context.SaveChangesAsync();

            int idTimeCode = timeCodeHistory.Id;

            logger.LogDebug($"Time code saved. Start saving accu history");

            foreach (Accu a in accu.accu)
            {
                _context.AccuStates.Add(new AccuHistory(idTimeCode, a.id, a.name, a.voltage, a.current, a.charge));
            }
            await _context.SaveChangesAsync();

            logger.LogDebug($"Accu history saved");

            return(Ok());
        }
Example #2
0
        public async Task<IActionResult> PutAccuHistory([FromRoute] int id, [FromBody] AccuHistory accuHistory)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != accuHistory.Id)
            {
                return BadRequest();
            }

            _context.Entry(accuHistory).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AccuHistoryExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }
Example #3
0
        public async Task <IActionResult> PostCarService([FromBody] AccuListWithTime accu)
        {
            logger.LogDebug($"Start saving car current state");
            if (!ModelState.IsValid)
            {
                logger.LogDebug($"Error: ModelState is not correct");
                return(BadRequest(ModelState));
            }

            logger.LogDebug($"Start saving time code");
            TimeCodeHistory timeCodeHistory = new TimeCodeHistory(accu.idCar, accu.time);

            _context.TimeCodes.Add(timeCodeHistory);
            await _context.SaveChangesAsync();

            int idTimeCode = timeCodeHistory.Id;

            logger.LogDebug($"Time code saved. Start saving accu history");

            foreach (Accu a in accu.accu)
            {
                AccuHistory accuHistory = new AccuHistory(idTimeCode, a.id, a.name, a.voltage, a.current, a.charge);
                _context.AccuStates.Add(accuHistory);
            }
            await _context.SaveChangesAsync();

            logger.LogDebug($"Accu history saved");

            return(Ok());
        }