public IActionResult Delete(int id, int page = 1, int sort = 1, bool ascending = true) { var oprema = _ctx.DodatnaOprema.Find(id); if (oprema == null) { return(NotFound()); } else { try { _ctx.Remove(oprema); _ctx.SaveChanges(); var result = new { message = $"Oprema sa sifrom {id} obrisana.", successful = true }; return(Json(result)); } catch (Exception e) { var result = new { message = $"Pogreska prilikom bisanja artikla {e.Message}", successful = false }; return(Json(result)); } } }
public void VratiIspravanBrojUsluga() { // Arrange var mockLogger = new Mock <ILogger <UslugeController> >(); var dbOptions = new DbContextOptionsBuilder <PI10Context>() .UseInMemoryDatabase(databaseName: "PI10Memory2") .Options; using (var context = new PI10Context(dbOptions)) { for (int i = 0; i < 50; i++) { context.Add(new Usluge { IdUsluge = i, NazivUsluge = "naz" }); } context.SaveChanges(); var controller = new UslugeController(context, options, mockLogger.Object); var tempDataMock = new Mock <ITempDataDictionary>(); controller.TempData = tempDataMock.Object; // Act var result = controller.Index(); // Assert var viewResult = Assert.IsType <ViewResult>(result); UslugeViewModel model = Assert.IsType <UslugeViewModel>(viewResult.Model); Assert.Equal(options.Value.PageSize, model.Usluge.Count()); } }
public IActionResult Create(Specifikacije specifikacija) { if (ModelState.IsValid) { try { _ctx.Add(specifikacija); _ctx.SaveChanges(); TempData[Constants.Message] = $"specifikacija uspjesno dodana.*"; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index))); } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); PrepareDropDownLists(); return(View(specifikacija)); } } else { return(View(specifikacija)); } }
public IActionResult Edit(Zahtjev zahtjev, int page = 1, int sort = 1, bool ascending = true) { if (zahtjev == null) { return(NotFound("Nema poslanih podataka")); } bool checkId = _ctx.Zahtjev.Any(m => m.IdZahtjeva == zahtjev.IdZahtjeva); if (!checkId) { return(NotFound($"Neispravan id zahtjeva: {zahtjev?.IdZahtjeva}")); } PrepareDropDownLists(); if (ModelState.IsValid) { try { _ctx.Update(zahtjev); _ctx.SaveChanges(); TempData[Constants.Message] = "zahtjev ažurirano."; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index), new { page, sort, ascending })); } catch (Exception exc) { ModelState.AddModelError(string.Empty, exc.Message); return(View(zahtjev)); } } else { return(View(zahtjev)); } }
public async Task <IActionResult> Create(Vozila vozilo, IFormFile slika) { int id = GetId(); if (ModelState.IsValid) { try { if (slika != null && slika.Length > 0) { using (MemoryStream stream = new MemoryStream()) { await slika.CopyToAsync(stream); byte[] image = stream.ToArray(); var slike = new Slike { IdSlike = id, SlikaBinary = image }; _ctx.Add(slike); } } vozilo.IdSlike = id; _ctx.Add(vozilo); _ctx.SaveChanges(); TempData[Constants.Message] = $"vozilo uspjesno dodano.*"; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index))); } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); PrepareDropDownLists(); return(View(vozilo)); } } else { return(View(vozilo)); } }
public IActionResult Create(Mjenjaci mjenjac) { if (ModelState.IsValid) { try { _ctx.Add(mjenjac); _ctx.SaveChanges(); TempData[Constants.Message] = $"Mjenjac {mjenjac.Naziv} uspjesno dodan.*"; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index))); } catch (Exception e) { TempData[Constants.ErrorOccurred] = $"Greska: {e.Message + e.StackTrace}"; return(View(mjenjac)); } } else { return(View(mjenjac)); } }
public IActionResult Create(VrsteGoriva vrsteGoriva) { if (ModelState.IsValid) { try { _ctx.Add(vrsteGoriva); _ctx.SaveChanges(); TempData[Constants.Message] = $"Drzava {vrsteGoriva.Naziv} uspjesno dodana.*"; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index))); } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); return(View(vrsteGoriva)); } } else { return(View(vrsteGoriva)); } }
public IActionResult Create(Odjeli odjel) { if (ModelState.IsValid) { try { _ctx.Add(odjel); _ctx.SaveChanges(); TempData[Constants.Message] = $"Odjel {odjel.Naziv} uspjesno dodan.*"; TempData[Constants.ErrorOccurred] = false; return(RedirectToAction(nameof(Index))); } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); return(View(odjel)); } } else { return(View(odjel)); } }