public void DeletePet(int id) { try { Pet pet = _context.Set <Pet>().Include(p => p.PetOwner).Include(p => p.PetAccomodation).FirstOrDefault(p => p.Id == id); if (pet == null) { throw new Exception("Pet não encontrado"); } pet.PetAccomodation.Available = true; _context.PetAccomodations.Update(pet.PetAccomodation); _context.Pets.Remove(pet); _context.PetsOwner.Remove(pet.PetOwner); _context.SaveChanges(); } catch (Exception ex) { throw new Exception("Ocorreu um erro ao tentar deletar"); } }
public PetType AddPetType(PetType petTypeToAdd) { var changeTracker = _context.ChangeTracker.Entries <PetType>(); _context.PetTypes.Attach(petTypeToAdd).State = EntityState.Added; _context.SaveChanges(); return(petTypeToAdd); }
public Owner AddOwner(Owner ownerToAdd) { var changeTracker = _context.ChangeTracker.Entries <Owner>(); _context.Attach(ownerToAdd).State = EntityState.Added; _context.SaveChanges(); return(ownerToAdd); }
public Pet CreatePet(Pet pet) { if (pet.PreviousOwner != null) { _ctx.Attach(pet.PreviousOwner); } _ctx.Attach(pet).State = EntityState.Added; _ctx.SaveChanges(); return(pet); }
public ActionResult Buy(int petId) { var db = new PetShopDbContext(); var pet = db.Pets .Where(p => p.Id == petId) .FirstOrDefault(); var userId = this.User.Identity.GetUserId(); if (pet == null || pet.IsBought || pet.OwnerId == userId) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var buying = new Buying { PetId = petId, BoughtOn = DateTime.Now, UserId = userId, Price = pet.Price }; pet.IsBought = true; db.BoughtPets.Add(buying); db.SaveChanges(); return(RedirectToAction("Details", new { id = pet.Id })); }
public ActionResult Edit(EditPetModel editPetModel) { if (this.ModelState.IsValid) { var db = new PetShopDbContext(); var pet = db.Pets .FirstOrDefault(p => p.Id == editPetModel.Id); var ownerId = this.User.Identity.GetUserId(); pet.Name = editPetModel.Name; pet.Age = editPetModel.Age; pet.AnimalType = editPetModel.AnimalType; pet.Breed = editPetModel.Breed; pet.Price = editPetModel.Price; pet.ImageUrl = editPetModel.ImageUrl; pet.Color = editPetModel.Color; pet.Size = editPetModel.Size; pet.Coat = editPetModel.Coat; pet.Characteristics = editPetModel.Characteristics; db.Entry(pet).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Details", new { id = editPetModel.Id })); } return(View(editPetModel)); }
public ActionResult Create(CreatePetModel model) { if (this.ModelState.IsValid) { var ownerId = this.User.Identity.GetUserId(); var pet = new Pet { Name = model.Name, Age = model.Age, AnimalType = model.AnimalType, Breed = model.Breed, Price = model.Price, ImageUrl = model.ImageUrl, Color = model.Color, Size = model.Size, Coat = model.Coat, Characteristics = model.Characteristics, OwnerId = ownerId }; var db = new PetShopDbContext(); db.Pets.Add(pet); db.SaveChanges(); return(RedirectToAction("Details", new { id = pet.Id })); } return(View(model)); }
/// <summary> /// Function to update inventory based on purchased items /// Internally the function uses a batch query so the command is only sent to the database once /// </summary> /// <param name="items">Array of items purchased</param> public void TakeStock(LineItemInfo[] items) { foreach (LineItemInfo item in items) { db.Inventories.Find(item.ItemId).Qty -= item.Quantity; } db.SaveChanges(); }
/// <summary> /// Insert a new account info the database /// </summary> /// <param name="acc">A thin data class containing all the new account information</param> public void Insert(AccountInfo acc) { //SqlParameter[] signOnParms = GetSignOnParameters(); //SqlParameter[] accountParms = GetAccountParameters(); //SqlParameter[] profileParms = GetProfileParameters(); //signOnParms[0].Value = acc.UserId; //signOnParms[1].Value = acc.Password; //SetAccountParameters(accountParms, acc); //SetProfileParameters(profileParms, acc); //using (SqlConnection conn = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC)) { // conn.Open(); // using (SqlTransaction trans = conn.BeginTransaction()) { // try { // SQLHelper.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_SIGNON, signOnParms); // SQLHelper.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_ACCOUNT, accountParms); // SQLHelper.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_PROFILE, profileParms); // trans.Commit(); // }catch { // trans.Rollback(); // throw; // } // } //} SignOn signon = new SignOn { UserName = acc.UserId, Password = acc.Password }; db.SignOns.Add(signon); Account account = new Account { UserId = acc.UserId, FirstName = acc.Address.FirstName, LastName = acc.Address.LastName, Email = acc.Email, Addr1 = acc.Address.Address1, Addr2 = acc.Address.Address2, City = acc.Address.City, Country = acc.Address.Country, Phone = acc.Address.Phone, Zip = acc.Address.Zip, State = acc.Address.State }; db.Accounts.Add(account); Profile profile = new Profile { UserId = acc.UserId, LangPref = acc.Language, FavCategory = acc.Category, MyListOpt = acc.IsShowFavorites ? 1 : 0, BannerOpt = acc.IsShowBanners ? 1 : 0 }; db.Profiles.Add(profile); db.SaveChanges(); }
public PetAccomodation SavePetAccomodation(PetAccomodation accomodation) { try { PetAccomodation accomodationCreate = _context.Add(accomodation).Entity; _context.SaveChanges(); return(accomodationCreate); } catch (Exception ex) { throw new Exception(ex.Message); } }
public Pet AddPet(Pet petToAdd) { //var petTypeChangeTracker = _context.ChangeTracker.Entries<PetType>(); //if (petToAdd.PetType != null && _context.ChangeTracker.Entries<PetType>() // .FirstOrDefault(petTypeEntry => petTypeEntry.Entity.OwnerId == petToAdd.PetType.OwnerId) == null) //{ // _context.Attach(petToAdd.PetType); //} //var petOwnerChangeTracker = _context.ChangeTracker.Entries<Owner>(); //if (petToAdd.Owner != null && _context.ChangeTracker.Entries<Owner>() // .FirstOrDefault(ownerEntry =>ownerEntry.Entity.OwnerId == petToAdd.Owner.OwnerId) == null) //{ // _context.Attach(petToAdd.Owner); //} _context.Attach(petToAdd).State = EntityState.Added; _context.SaveChanges(); return(petToAdd); }
public ActionResult Delete(int Id) { var db = new PetShopDbContext(); var pet = db.Pets .Where(p => p.Id == Id) .FirstOrDefault(); var user = this.User.Identity.GetUserId(); if (pet == null || user != pet.OwnerId) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } db.Pets.Remove(pet); db.SaveChanges(); return(Redirect("/Pets/All")); }
public User AddUser(User userToAdd) { _context.Attach(userToAdd).State = EntityState.Added; _context.SaveChanges(); return(userToAdd); }
public Owner CreateOwner(Owner owner) { _ctx.Attach(owner).State = EntityState.Added; _ctx.SaveChanges(); return(owner); }
public PetColor AddPetColor(PetColor petColorToAdd) { _context.Attach(petColorToAdd).State = EntityState.Added; _context.SaveChanges(); return(petColorToAdd); }
public Color AddColor(Color colorToAdd) { _context.Attach(colorToAdd).State = EntityState.Added; _context.SaveChanges(); return(colorToAdd); }