public bool CreateRental(RentalCreate model) // this will create an instance of Note. { using (var ctx = new ApplicationDbContext()) { var entity = ctx.Members.Single( c => c.Id == model.MemberId); var kits = ctx.Kits.Where( m => model.KitId.Equals(m.Id)).ToList(); foreach (var kit in kits) { var rental = new Rental() { MemberId = memberId, Kit = kit, StartDate = DateTime.Now }; } { ctx.Rentals.Add(entity); return(ctx.SaveChanges() == 1); } } }
public bool CreateRental(RentalCreate model) { var entity = new Rental() { Id = model.Id, KitId = model.KitId, RentalPrice = model.RentalPrice, StartDate = model.StartDate, EndDate = model.EndDate, }; using (var ctx = new ApplicationDbContext()) { ctx.Rentals.Add(entity); return(ctx.SaveChanges() == 1); } }
//method to create a rental by passing in the required information public bool CreateRental(RentalCreate model) { var entity = new Rental() { ToolId = model.ToolId, CustomerId = model.CustomerId, ScheduledStartDate = model.ScheduledStartDate, ScheduledEndDate = model.ScheduledEndDate }; //end of defining attributes for new Rental using (var ctx = new ApplicationDbContext()) { ctx.Rentals.Add(entity); return(ctx.SaveChanges() == 1); } //end of using } //end of method CreateRental
}//end of method CreateRentalService //post a rental /// <summary> /// Post a new Rental object to the Database /// </summary> /// <param name="Rental">Contains the required fields for a Rental object</param> /// <returns></returns> public IHttpActionResult Post(RentalCreate Rental) { //start by checking that the model state is valid if (!ModelState.IsValid) { return(BadRequest(ModelState)); }//end of if state valid //if it's valid create a RentalService var service = CreateRentalService(); //now we are going to call the createRental method inside an if statement //if it succeeds great, and if it doesnt we return and InternalServerError if (!service.CreateRental(Rental)) { //didn't work, return problem return(InternalServerError()); }//end of if not createRental (no success) //if we get here it worked, yay return(Ok()); }//end of Post method
public CreateNewRental(RentalCreate model) { using (var ctx = new ApplicationDbContext()) { var member = ctx.Members.Single( c => c.Id == model.MemberId); var kits = ctx.Kits.Where( m => model.KitId.Equals(m.Id)).ToList(); foreach (var kit in kits) { var rental = new Rental { Member = member, Kit = kit, StartDate = DateTime.Now }; ctx.Rentals.Add(rental); return(ctx.SaveChanges() == 1); } } }