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()); }
public async Task <IActionResult> PutTimeCodeHistory([FromRoute] int id, [FromBody] TimeCodeHistory timeCodeHistory) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != timeCodeHistory.Id) { return(BadRequest()); } _context.Entry(timeCodeHistory).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TimeCodeHistoryExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
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()); }
public async Task <IActionResult> PostTimeCodeHistory([FromBody] TimeCodeHistory timeCodeHistory) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.TimeCodes.Add(timeCodeHistory); await _context.SaveChangesAsync(); return(CreatedAtAction("GetTimeCodeHistory", new { id = timeCodeHistory.Id }, timeCodeHistory)); }