public async Task <IActionResult> PutVideotape([FromRoute] int id, [FromBody] Videotape videotape) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != videotape.videotapeId) { return(BadRequest()); } try { vts.EditVideotape(videotape, id); } catch (DbUpdateConcurrencyException) { if (!VideotapeExists(id)) { return(NotFound()); } else { throw; } } return(Ok(videotape)); }
/** * Adds videotape to database. */ public void addVideotape(Videotape tape) { var latestId = _context.Videotapes.Max(p => p.videotapeId); tape.videotapeId = latestId + 1; _context.Videotapes.Add(tape); _context.SaveChangesAsync(); }
/** * Deletes videotape from database. * Also deletes review and borrow history of the deleted videotape. */ public void deleteVideotape(Videotape tape) { IEnumerable <Borrow> borrows = _context.Borrows.Where(e => e.videotapeId == tape.videotapeId); IEnumerable <Review> reviews = _context.Reviews.Where(e => e.videotapeId == tape.videotapeId); _context.Borrows.RemoveRange(borrows); _context.Reviews.RemoveRange(reviews); _context.Videotapes.Remove(tape); _context.SaveChangesAsync(); }
public async Task <IActionResult> PostVideotape([FromBody] Videotape videotape) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } vts.addVideotape(videotape); return(CreatedAtAction("GetVideotape", new { id = videotape.videotapeId }, videotape)); }
/** * Returns videotape with ID and its borrow history. */ public object getVideotapeAndBorrow(int id) { Videotape videotape = _context.Videotapes.SingleOrDefault(e => e.videotapeId == id); // If videotape no longer exists in database. if (videotape == null) { return(null); } IEnumerable <Borrow> borrows = _context.Borrows.Where(e => e.videotapeId == id); return(new { videotape, borrows }); }
/** * A recursive function that returns a videotape recommendation if the user has not borrowed it before. */ public Videotape getRecommendation(int userId) { int total = _context.Videotapes.Count(); Random r = new Random(); int offset = r.Next(0, total); Videotape result = _context.Videotapes.Skip(offset).FirstOrDefault(); if (_context.Borrows.Any(e => e.userId == userId & e.videotapeId == result.videotapeId)) { getRecommendation(userId); } return(result); }
/** * Modify videotapes's data with ID. */ public void EditVideotape(Videotape tape, int id) { tape.videotapeId = id; _context.Entry(tape).State = EntityState.Modified; _context.SaveChangesAsync(); }