private void AddAnimalProduct() { var elements = ReadElements(); AnimalProduct animalProduct = new AnimalProduct(elements[0], elements[1], decimal.Parse(elements[2]), int.Parse(elements[3])); animalProductController.Add(animalProduct); Console.WriteLine("The product was successfully added!"); }
/// <summary> /// Updates an animal product. /// </summary> /// <param name="animalProduct">the animal product that will be updated</param> public void Update(AnimalProduct animalProduct) { var item = context.AnimalProducts.Find(animalProduct.Id); if (item != null) { context.Entry(item).CurrentValues.SetValues(animalProduct); context.SaveChanges(); } }
public void AddAnimalProduct_Add_A_AnimalProduct() { var mockSet = new Mock <DbSet <AnimalProduct> >(); var animalProduct = new AnimalProduct(); var mockContext = new Mock <ShopContext>(); mockContext.Setup(m => m.AnimalProducts).Returns(mockSet.Object); var controller = new AnimalProductController(mockContext.Object); controller.Add(animalProduct); mockSet.Verify(m => m.Add(It.IsAny <AnimalProduct>()), Times.Once()); mockContext.Verify(m => m.SaveChanges(), Times.Once()); }
/// <summary> /// Adds an animal product in database. /// </summary> /// <param name="animalProduct">the animal product that will be added</param> public void Add(AnimalProduct animalProduct) { context.AnimalProducts.Add(animalProduct); context.SaveChanges(); }
public async Task <IActionResult> Create([Bind("AnimalID,Name,DateOfBirth,Disinfection,Neutered,Description,Foto,Picture,AnimalTypeFK,AnimalRaceFK,OwnerFK")] Animal animal) { if (!GetAuthorization(8, 'c')) { return(NotFound()); } ViewBag.Permission = getPermissions(); if (!checkValues(animal)) { setViewBags(-1); return(View(animal)); } if (ModelState.IsValid) { string selectedProducts = Request.Form["checkProduct"].ToString(); string[] selectedProductsList = selectedProducts.Split(','); string selectedGodfathers = Request.Form["checkGodfather"].ToString(); string[] selectedGodfatherList = selectedGodfathers.Split(','); Animal animalAdd = new Animal() { Name = animal.Name, DateOfBirth = animal.DateOfBirth, Disinfection = animal.Disinfection, Neutered = animal.Neutered, Description = animal.Description, AnimalTypeFK = animal.AnimalTypeFK, AnimalRaceFK = animal.AnimalRaceFK, OwnerFK = animal.OwnerFK }; _context.Add(animalAdd); _context.SaveChanges(); if (animal.Foto != null) { System.IO.Directory.CreateDirectory(hostingEnvironment.WebRootPath + "/images/Galeria_" + animalAdd.AnimalID); var uniqueFileName = GetUniqueFileName(animal.Foto.FileName); var uploads = Path.Combine(hostingEnvironment.WebRootPath, "images/Galeria_" + animalAdd.AnimalID); var filePath = Path.Combine(uploads, uniqueFileName); animal.Foto.CopyTo(new FileStream(filePath, FileMode.Create)); Images image = new Images() { AnimalFK = animalAdd.AnimalID, Name = animal.Foto.Name, Length = animal.Foto.Length, FileName = uniqueFileName, ContentType = animal.Foto.ContentType, ContentDisposition = animal.Foto.ContentDisposition }; _context.Add(image); _context.SaveChanges(); //to do : Save uniqueFileName to your db table } if (selectedProductsList[0] != "") { foreach (var temp in selectedProductsList) { int prodKey = Convert.ToInt32(temp); AnimalProduct aniProd = new AnimalProduct() { AnimalFK = animalAdd.AnimalID, ProductFK = prodKey }; _context.Add(aniProd); _context.SaveChanges(); } } if (selectedGodfatherList[0] != "") { foreach (var key in selectedGodfatherList) { int godkey = Convert.ToInt32(key); AnimalUsers aniUsers = new AnimalUsers() { AnimalFK = animalAdd.AnimalID, UsersFK = godkey }; _context.Add(aniUsers); _context.SaveChanges(); } } await _context.SaveChangesAsync(); TempData["Message"] = "Animal criado com sucesso!"; return(RedirectToAction(nameof(Index))); } setViewBags(-1); return(View(animal)); }
public async Task <IActionResult> Edit(int id, [Bind("AnimalID,Name,DateOfBirth,Disinfection,Neutered,Description,Foto,Picture,AnimalTypeFK,AnimalRaceFK,OwnerFK")] Animal animal) { if (!GetAuthorization(8, 'u')) { return(NotFound()); } ViewBag.Permission = getPermissions(); if (id != animal.AnimalID) { return(NotFound()); } if (!checkValues(animal)) { setViewBags(id); return(View(animal)); } if (ModelState.IsValid) { try { string selectedProducts = Request.Form["checkProduct"].ToString(); string[] selectedProductsList = selectedProducts.Split(','); string selectedGodfathers = Request.Form["checkGodfather"].ToString(); string[] selectedGodfatherList = selectedGodfathers.Split(','); if (animal.Foto != null) { var result = _context.Images.Where(e => e.AnimalFK == animal.AnimalID); if (result.Any()) { Images old = result.First(); System.IO.File.Delete(hostingEnvironment.WebRootPath + "/images/Galeria_" + animal.AnimalID + "/" + old.FileName); _context.Images.Remove(old); _context.SaveChanges(); } else { System.IO.Directory.CreateDirectory(hostingEnvironment.WebRootPath + "/images/Galeria_" + animal.AnimalID); } var uploads = Path.Combine(hostingEnvironment.WebRootPath, "images/Galeria_" + animal.AnimalID); var uniqueFileName = GetUniqueFileName(animal.Foto.FileName); var filePath = Path.Combine(uploads, uniqueFileName); animal.Foto.CopyTo(new FileStream(filePath, FileMode.Create)); Images image = new Images() { AnimalFK = animal.AnimalID, Name = animal.Foto.Name, Length = animal.Foto.Length, FileName = uniqueFileName, ContentType = animal.Foto.ContentType, ContentDisposition = animal.Foto.ContentDisposition }; _context.Add(image); _context.SaveChanges(); } _context.Update(animal); _context.SaveChanges(); removeAnimalProducts(id); if (selectedProductsList[0] != "") { foreach (var temp in selectedProductsList) { int prodKey = Convert.ToInt32(temp); AnimalProduct aniProd = new AnimalProduct() { AnimalFK = id, ProductFK = prodKey }; _context.Add(aniProd); _context.SaveChanges(); } } removeGodFathers(id); if (selectedGodfatherList[0] != "") { foreach (var temp in selectedGodfatherList) { int godkey = Convert.ToInt32(temp); AnimalUsers aniUsers = new AnimalUsers() { AnimalFK = id, UsersFK = godkey }; _context.Add(aniUsers); _context.SaveChanges(); } } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AnimalExists(animal.AnimalID)) { return(NotFound()); } else { throw; } } TempData["Message"] = "Animal editado com sucesso!"; return(RedirectToAction(nameof(Index))); } setViewBags(id); return(View(animal)); }