public Animal Post([FromBody] Animal animal) //creates new animal and returns created animal { _context.Animal.Add(animal); _context.SaveChanges(); return(animal); }
public async Task <IActionResult> Create([Bind("DonationID,DateOfDonation,UsersFK")] DonationIndexViewModel donationIVM) { if (!GetAuthorization(5, 'c')) { return(NotFound()); } ViewBag.Permission = getPermissions(); if (!checkValues(donationIVM)) { return(Redirect(nameof(Create))); } if (ModelState.IsValid) { string selected = Request.Form["checkProduct"].ToString(); string[] selectedList = selected.Split(','); Donation donation = new Donation { DateOfDonation = donationIVM.DateOfDonation, UsersFK = donationIVM.UsersFK }; _context.Add(donation); _context.SaveChanges(); if (selectedList[0] != "") { foreach (var temp in selectedList) { int prodKey = Convert.ToInt32(temp); int newQuant = 0; if (Request.Form["quantityProduct " + Convert.ToInt32(temp)] != "") { newQuant = Convert.ToInt32(Request.Form["quantityProduct " + Convert.ToInt32(temp)].ToString()); } DonationProduct donationProduct = new DonationProduct { DonationFK = donation.DonationID, ProductFK = prodKey, Quantity = newQuant }; var prod = new Product { ProductID = prodKey }; prod.Quantity = _context.Products.Where(e => e.ProductID == prodKey).FirstOrDefault().Quantity + newQuant; _context.Entry(prod).Property("Quantity").IsModified = true; _context.SaveChanges(); _context.Add(donationProduct); } } await _context.SaveChangesAsync(); TempData["Message"] = "Doacao criada com sucesso!"; return(RedirectToAction(nameof(Index))); } return(View(donationIVM)); }
public void DeleteAnimal(int animalId, int shelterId) { var data = _context.Shelters .Include(Shelter => Shelter.Animals) .FirstOrDefault(x => x.Id == shelterId)?.Animals; Animal animal = _context.Animals.Find(animalId); _context.Animals.Remove(animal); _context.SaveChanges(); }
public Shared.Animal DeleteAnimal(int shelterId, int animalId) { var pickedAnimal = _context.Animals .FirstOrDefault(x => x.Id == animalId && x.ShelterId == shelterId); _context.Remove(pickedAnimal); _context.SaveChanges(); return(pickedAnimal); }
public Pet CreatePet(Pet pet) { if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.BadRequest); } _context.Pets.Add(pet); _context.SaveChanges(); return(pet); }
public void DeleteAnimalByshelterIdAndId(int shelterId, int animalId) { var animal = _context.Animals .FirstOrDefault(x => x.ShelterId == shelterId && x.Id == animalId); _context.Animals .Remove(animal); _context.SaveChanges(); }
public void Seed(ShelterContext context) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); context.Animal.AddRange( new Animal() { Age = 2, Weight = 10, Size = 8, Name = "puszek", Added = new DateTime(2019, 5, 9, 9, 32, 52), Strain = "pies" }, new Animal() { Age = 6, Weight = 11, Size = 3, Name = "burek", Added = new DateTime(2017, 7, 15, 6, 39, 52), Strain = "pies" }, new Animal() { Age = 4, Weight = 18, Size = 2, Name = "azor", Added = new DateTime(2018, 12, 15, 15, 10, 52), Strain = "pies" }, new Animal() { Age = 9, Weight = 5, Size = 7, Name = "mysia", Added = new DateTime(2019, 11, 8, 9, 20, 52), Strain = "chomik" }, new Animal() { Age = 2, Weight = 9, Size = 4, Name = "riko", Added = new DateTime(2018, 10, 21, 21, 50, 52), Strain = "kot" }, new Animal() { Age = 2, Weight = 6, Size = 2, Name = "miki", Added = new DateTime(2019, 11, 8, 6, 7, 52), Strain = "kot" }, new Animal() { Age = 12, Weight = 1, Size = 3, Name = "burek", Added = new DateTime(2017, 6, 9, 11, 12, 52), Strain = "pies" }, new Animal() { Age = 4, Weight = 10, Size = 10, Name = "dino", Added = new DateTime(2019, 3, 2, 8, 30, 52), Strain = "papuga" } ); context.SaveChanges(); }
public ActionResult Save(Pet pet) { if (!ModelState.IsValid) { var viewModel = new PetViewModel { Pet = pet, Shelters = _context.Shelters.ToList() }; return(View("PetForm", viewModel)); } if (pet.Id == 0) { _context.Pets.Add(pet); } else { var petInDb = _context.Pets.Single(p => p.Id == pet.Id); petInDb.PetName = pet.PetName; petInDb.Species = pet.Species; petInDb.Breed = pet.Breed; petInDb.IsAdopted = pet.IsAdopted; petInDb.Description = pet.Description; petInDb.Shelter = pet.Shelter; petInDb.AddedDate = pet.AddedDate; petInDb.PhotoPath = pet.PhotoPath; } // TODO: Naprawić dodawanie/edycję! Problem: pole HiddenFor w PetForm _context.SaveChanges(); return(RedirectToAction("Index", "Pets")); }
public void Post([FromBody] Animal animal) { _db.Animals.Add(animal); _db.SaveChanges(); }
public void Post([FromBody] Cat cat) { _db.Cats.Add(cat); _db.SaveChanges(); }
public ActionResult Create(Details details) { _db.Details.Add(details); _db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create(Animal animal) { _db.Animal.Add(animal); _db.SaveChanges(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Create([Bind("RoleID,RoleName")] Roles roles) { System.Diagnostics.Debug.WriteLine("******************************************************"); System.Diagnostics.Debug.WriteLine("Entrou"); System.Diagnostics.Debug.WriteLine("******************************************************"); if (!GetAuthorization(2, 'c')) { return(NotFound()); } ViewBag.Permission = getPermissions(); if (!checkValues(roles)) { setViewBags(-1); return(View(roles)); } if (ModelState.IsValid) { _context.Add(roles); _context.SaveChanges(); var componenets = _context.Components.ToList(); foreach (var comp in componenets) { string selectedOptions = Request.Form[comp.Name].ToString(); string[] selectedOptionsList = selectedOptions.Split(','); RoleAuthorization rAutho = new RoleAuthorization { ComponentFK = comp.ComponentID, RoleFK = roles.RoleID, Create = false, Read = false, Update = false, Delete = false }; if (selectedOptionsList[0] != "") { foreach (var val in selectedOptionsList) { int id = Int32.Parse(val); switch (id) { case 1: rAutho.Create = true; break; case 2: rAutho.Read = true; break; case 3: rAutho.Update = true; break; case 4: rAutho.Delete = true; break; default: break; } } } if (rAutho.Delete || rAutho.Create || rAutho.Update) { rAutho.Read = true; } _context.Add(rAutho); _context.SaveChanges(); } await _context.SaveChangesAsync(); TempData["Message"] = "Permissão criada com sucesso!"; return(RedirectToAction(nameof(Index))); } TempData["Message"] = "Model de errado aconteceu!"; setViewBags(-1); return(View(roles)); }
public async Task <IActionResult> Login([Bind("Email,Password")] Users users) { try { if (users.Email.Equals("")) { TempData["Message"] = "Email or Password incorreto!"; ModelState.AddModelError("Email", "Email or Password incorreto!"); return(View("~/Views/Home/Index.cshtml")); } var user = await _context.Users.SingleAsync(i => i.Email == users.Email); if (user != null) { if (user.ConfirmedEmail == false) { TempData["Message"] = "Este email ainda não foi confirmado! Por favor vá ao seu email e siga as instruções!"; return(View("~/Views/Home/Index.cshtml")); } if (user.Password != users.Password) { TempData["Message"] = "Password Errada!"; ModelState.AddModelError("Email", "Email or Password incorreto!"); return(View("~/Views/Home/Index.cshtml")); } LoginSV(user.Name, user.UserID.ToString()); TempData["Message"] = "Login efetuado com sucesso!"; string date31string = DateTime.Today.ToString("yyyy-MM-dd"); DateTime today = DateTime.ParseExact(date31string, "yyyy-MM-dd", null); var checkDate = _context.LoginStatistic.Where(e => e.DateStatistic.Equals(today)).FirstOrDefault(); if (checkDate != null) { LoginStatistic update = checkDate; update.Count += 1; _context.LoginStatistic.Update(update); } else { //TimeSpan difference = end - start; LoginStatistic newStatistic = new LoginStatistic { DateStatistic = today, Count = 1 }; var lastDate = _context.LoginStatistic.LastOrDefault(); if (lastDate != null) { TimeSpan difference = today - lastDate.DateStatistic; if (difference.Days != 1) { int daysMissing = difference.Days; while (daysMissing != 1) { LoginStatistic fillTable = new LoginStatistic { DateStatistic = DateTime.ParseExact(DateTime.Today.AddDays(-daysMissing).ToString("yyyy-MM-dd"), "yyyy-MM-dd", null), Count = 0 }; _context.LoginStatistic.Add(fillTable); _context.SaveChanges(); --daysMissing; } } } _context.LoginStatistic.Add(newStatistic); } _context.SaveChanges(); return(RedirectToAction("Index", "Home", null)); } TempData["Message"] = "Email or Password incorreto!"; ModelState.AddModelError("Email", "Email or Password incorreto!"); return(View("~/Views/Home/Index.cshtml")); } catch (Exception ex) { TempData["Message"] = "Email or Password incorreto!"; ModelState.AddModelError("Email", "Email or Password incorreto!"); return(View("~/Views/Home/Index.cshtml")); } }
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 void Post([FromBody] Dog dog) { _db.Dogs.Add(dog); _db.SaveChanges(); }