public async Task EditAsync(LightPoleModel model) { if (await _context.LightPoles.AnyAsync(x => x.SerialNumber == model.SerialNumber && x.Id != model.Id)) { throw new Exception("Já existe um posto de luz com esse código de série"); } var lightPole = await _context.LightPoles .Include(x => x.Localization) .FirstOrDefaultAsync(x => x.Id == model.Id); if (lightPole == null) { throw new Exception("Poste de luz não encontrado"); } lightPole.Number = model.NumberHouse; lightPole.SerialNumber = model.SerialNumber; lightPole.Active = model.Active; try { _context.LightPoles.Update(lightPole); await _context.SaveChangesAsync(); } catch { throw new Exception("Erro. Por favor tente novamente"); } }
public async Task RegisterAsync(LightPoleModel model) { if (await _context.LightPoles.AnyAsync(x => x.SerialNumber == model.SerialNumber)) { throw new Exception("Poste de luz já instalado"); } var localization = await _context.Localizations.FirstOrDefaultAsync(x => x.Id == model.LocId); if (localization == null) { throw new Exception("Localização não encontrada"); } var lightPole = new LightPole(localization, model.NumberHouse, model.Active, model.SerialNumber); try { await _context.LightPoles.AddAsync(lightPole); await _context.SaveChangesAsync(); } catch { throw new Exception("Erro. Por favor tente novamente"); } }
public async Task <IActionResult> Edit(LightPoleModel model) { try { await _appLightPole.EditAsync(model); ViewBag.Success = true; ViewBag.Message = "Informações do poste de luz editadas"; return(View(model)); } catch (Exception x) { ViewBag.Success = false; ViewBag.Message = x.Message; return(View(model)); } }
public async Task <IActionResult> Register(LightPoleModel model) { LoadData(); if (!ModelState.IsValid) { return(View(model)); } try { await _appLightPole.RegisterAsync(model); TempData["Success"] = true; TempData["Message"] = "Poste instalado com sucesso"; return(RedirectToAction("Index")); } catch (Exception x) { ViewBag.Success = false; ViewBag.Message = x.Message; return(View(model)); } }