Ejemplo n.º 1
0
        public ActionResult Rent(VideoRentViewModel model)
        {
            Video video = db.Videos.Include(v => v.Copies)
                          .SingleOrDefault(v => v.ID == model.ID && v.Copies.Where(c => c.RentedDate == null).Count() > 0);

            if (video == null)
            {
                return(HttpNotFound());
            }

            DateTime  now  = DateTime.UtcNow;
            VideoCopy copy = video.Copies.Where(c => c.RentedDate == null).First();

            copy.RentedDate      = now;
            copy.RenterID        = UserID;
            copy.RentedDays      = model.Days;
            db.Entry(copy).State = EntityState.Modified;

            UserHistory history = new UserHistory();

            history.Copy         = copy;
            history.ID           = Guid.NewGuid();
            history.PointsEarned = (video.Age == VideoAge.New ? 2 : 1);
            history.RentedDate   = now;
            history.RentedDays   = model.Days;
            history.UserID       = UserID;
            db.UserHistory.Add(history);
            db.SaveChanges();

            return(Json(true, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 2
0
        public ActionResult Return(Guid id)
        {
            VideoCopy copy = db.VideoCopies.Include(c => c.Video).Where(c => c.ID == id && c.RentedDate != null).SingleOrDefault();

            if (copy == null) // Copy not rented out
            {
                return(HttpNotFound());
            }

            copy.RentedDate      = null;
            copy.RenterID        = null;
            copy.RentedDays      = 0;
            db.Entry(copy).State = EntityState.Modified;

            UserHistory historyEntry = db.UserHistory.Where(h => h.CopyID == copy.ID && h.ReturnedDate == null).SingleOrDefault();

            if (historyEntry == null)
            {
                // Not rented out to this user
                return(HttpNotFound());
            }
            historyEntry.ReturnedDate = DateTime.UtcNow;
            db.SaveChanges();

            return(RedirectToAction("Details", "Videos", new { id = copy.VideoID }));
        }
Ejemplo n.º 3
0
        public ActionResult DeleteConfirmed(Guid id)
        {
            VideoCopy videoCopy = db.VideoCopies.Find(id);

            db.VideoCopies.Remove(videoCopy);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 4
0
 public ActionResult Edit([Bind(Include = "ID,RentedDate,RentedDays")] VideoCopy videoCopy)
 {
     if (ModelState.IsValid)
     {
         db.Entry(videoCopy).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(videoCopy));
 }
Ejemplo n.º 5
0
        public ActionResult Create([Bind(Include = "ID,RentedDate,RentedDays")] VideoCopy videoCopy)
        {
            if (ModelState.IsValid)
            {
                videoCopy.ID = Guid.NewGuid();
                db.VideoCopies.Add(videoCopy);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(videoCopy));
        }
Ejemplo n.º 6
0
        // GET: VideoCopies/Details/5
        public ActionResult Details(Guid?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            VideoCopy videoCopy = db.VideoCopies.Find(id);

            if (videoCopy == null)
            {
                return(HttpNotFound());
            }
            return(View(videoCopy));
        }
Ejemplo n.º 7
0
        public IHttpActionResult Create(Guid videoID)
        {
            Video video = db.Videos.Find(videoID);

            if (video == null)
            {
                return(NotFound());
            }

            // Shortcut, because we don't need to add any data to a new copy.
            VideoCopy copy = new VideoCopy();

            copy.Video = video;
            copy.ID    = Guid.NewGuid();
            db.VideoCopies.Add(copy);
            db.SaveChanges();
            return(Ok <VideoCopy>(copy));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// deflates (zlib) a VideoCopy, returning a byte array suitable for insertion into a JMD file
        /// the byte array includes width and height dimensions at the beginning
        /// this is run asynchronously for speedup, as compressing can be slow
        /// </summary>
        /// <param name="v">video frame to compress</param>
        /// <returns>zlib compressed frame, with width and height prepended</returns>
        byte[] GzipFrame(VideoCopy v)
        {
            MemoryStream m = new MemoryStream();

            // write frame height and width first
            m.WriteByte((byte)(v.BufferWidth >> 8));
            m.WriteByte((byte)(v.BufferWidth & 255));
            m.WriteByte((byte)(v.BufferHeight >> 8));
            m.WriteByte((byte)(v.BufferHeight & 255));
            var g = new DeflaterOutputStream(m, new Deflater(token.compressionlevel));

            g.IsStreamOwner = false;             // leave memory stream open so we can pick its contents
            g.Write(v.VideoBuffer, 0, v.VideoBuffer.Length);
            g.Flush();
            g.Close();
            byte[] ret = m.GetBuffer();
            Array.Resize(ref ret, (int)m.Length);
            m.Close();
            return(ret);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// deflates (zlib) a VideoCopy, returning a byte array suitable for insertion into a JMD file
        /// the byte array includes width and height dimensions at the beginning
        /// this is run asynchronously for speedup, as compressing can be slow
        /// </summary>
        /// <param name="v">video frame to compress</param>
        /// <returns>zlib compressed frame, with width and height prepended</returns>
        private byte[] GzipFrame(VideoCopy v)
        {
            var m = new MemoryStream();

            // write frame height and width first
            m.WriteByte((byte)(v.BufferWidth >> 8));
            m.WriteByte((byte)(v.BufferWidth & 255));
            m.WriteByte((byte)(v.BufferHeight >> 8));
            m.WriteByte((byte)(v.BufferHeight & 255));

            var g = new GZipStream(m, GetCompressionLevel(_token.CompressionLevel), true);             // leave memory stream open so we can pick its contents

            g.Write(v.VideoBuffer, 0, v.VideoBuffer.Length);
            g.Flush();
            g.Close();

            byte[] ret = m.GetBuffer();
            Array.Resize(ref ret, (int)m.Length);
            m.Close();
            return(ret);
        }
Ejemplo n.º 10
0
        private double GetLateFee(VideoCopy copy)
        {
            double   lateFee = 0;
            DateTime dueDate = copy.RentedDate != null ? ((DateTime)copy.RentedDate).AddDays(copy.RentedDays) : default(DateTime);

            if (dueDate != default(DateTime) && dueDate < DateTime.UtcNow)
            {
                // Late return

                // I'm assuming that for old films, we will still charge
                // late rent even if their rental period is shorter than the
                // minimum rental time. IE, if they say the want to rent for 2 days,
                // they still pay the minimum of 5 days, and if they return after 4 days,
                // they get charged for 2 days late rent.
                TimeSpan overdue = DateTime.UtcNow - dueDate;


                lateFee = (overdue.Days + 1) * (copy.Video.Age == VideoAge.New ? GlobalValues.NewReleasePrice : GlobalValues.RegularReleasePrice);
            }
            return(lateFee);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Rent a particual video, if available.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IHttpActionResult Rent(VideoRentViewModel model)
        {
            // Note that I would prefer to have a service deal with the renting
            // and other operations on videos or video copies because
            // this code is duplicated in the api service. I'm not doing that here
            // to avoid complexity.
            //
            // The call the could be service.Rent(model.ID, UserID, days); for both
            // uses.
            Video video = db.Videos.Include(v => v.Copies)
                          .SingleOrDefault(v => v.ID == model.ID && v.Copies.Where(c => c.RentedDate == null).Count() > 0);

            if (video == null)
            {
                return(NotFound());
            }

            DateTime  now  = DateTime.UtcNow;
            VideoCopy copy = video.Copies.Where(c => c.RentedDate == null).First();

            copy.RentedDate      = now;
            copy.RenterID        = UserID;
            copy.RentedDays      = model.Days;
            db.Entry(copy).State = EntityState.Modified;

            UserHistory history = new UserHistory();

            history.Copy         = copy;
            history.ID           = Guid.NewGuid();
            history.PointsEarned = (video.Age == VideoAge.New ? 2 : 1);
            history.RentedDate   = now;
            history.RentedDays   = model.Days;
            history.UserID       = UserID;
            db.UserHistory.Add(history);
            db.SaveChanges();

            return(Json(history));
        }
Ejemplo n.º 12
0
        // GET: VideoCopies/Create
        public ActionResult Create(Guid videoID)
        {
            Video video = db.Videos.Find(videoID);

            if (video == null)
            {
                return(HttpNotFound());
            }

            // Shortcut, because we don't need to add any data to a new copy.
            VideoCopy copy = new VideoCopy();

            copy.Video = video;
            copy.ID    = Guid.NewGuid();
            db.VideoCopies.Add(copy);
            db.SaveChanges();
            return(RedirectToAction("Details", "Videos", new { id = video.ID }));

            // This shows as not reachable because of the above shortcut.
            var model = new VideoFrontPageViewModel();

            return(View());
        }
Ejemplo n.º 13
0
		/// <summary>
		/// deflates (zlib) a VideoCopy, returning a byte array suitable for insertion into a JMD file
		/// the byte array includes width and height dimensions at the beginning
		/// this is run asynchronously for speedup, as compressing can be slow
		/// </summary>
		/// <param name="v">video frame to compress</param>
		/// <returns>zlib compressed frame, with width and height prepended</returns>
		byte[] GzipFrame(VideoCopy v)
		{
			MemoryStream m = new MemoryStream();
			// write frame height and width first
			m.WriteByte((byte)(v.BufferWidth >> 8));
			m.WriteByte((byte)(v.BufferWidth & 255));
			m.WriteByte((byte)(v.BufferHeight >> 8));
			m.WriteByte((byte)(v.BufferHeight & 255));
			var g = new DeflaterOutputStream(m, new Deflater(token.compressionlevel));
			g.IsStreamOwner = false; // leave memory stream open so we can pick its contents
			g.Write(v.VideoBuffer, 0, v.VideoBuffer.Length);
			g.Flush();
			g.Close();
			byte[] ret = m.GetBuffer();
			Array.Resize(ref ret, (int)m.Length);
			m.Close();
			return ret;
		}