Example #1
0
 public ActionResult <MoviePriceLog> PostMoviePriceLog(MoviePriceLog oLog)
 {
     oLogRepository.Create(oLog);
     return(CreatedAtAction("GetMoviePriceLog", new { iLogID = oLog.MoviePriceLogID }, oLog));
 }
Example #2
0
        /// <summary>
        /// iResult = -1 -> Non existing record
        /// iResult = 0 -> Not Updated
        /// iResult = 1 -> Updated
        /// </summary>
        /// <param name="oMovieEntity"></param>
        /// <returns></returns>
        public int UpdateMovie(int iExistingID, Movie oMovieEntity, out string sErrorMessage)
        {
            int iResult = 0;

            sErrorMessage = "";

            if (iExistingID == 0)
            {
                iResult = -1;
                return(iResult);
            }

            var oExistingMovie = oContext.Movies.AsNoTracking().Where(x => x.MovieID == iExistingID).FirstOrDefault();

            if (oExistingMovie == null)
            {
                iResult = -1;
                return(iResult);
            }

            // Only Admins can update Availabilty
            var oAdminRole    = oContext.Roles.Where(x => x.Name.Trim().ToLower() == "administrator").FirstOrDefault();
            var oUserModifies = oContext.Users.AsNoTracking().Where(x => x.UserID == oMovieEntity.UpdatedUserID).FirstOrDefault();

            if (oExistingMovie.Available != oMovieEntity.Available)
            {
                if (oUserModifies.RoleID != oAdminRole.RoleID)
                {
                    sErrorMessage = "Only Admin users can update Availability";
                    return(iResult);
                }
            }

            // Update current Movie
            oMovieEntity.Created = oExistingMovie.Created;
            oMovieEntity.Updated = DateTime.Now;
            Update(oMovieEntity);

            // Verifiy if a Log must be created
            bool bCreateLog = false;

            if (oMovieEntity.RentalPrice != oExistingMovie.RentalPrice ||
                oMovieEntity.SalePrice != oExistingMovie.SalePrice ||
                oMovieEntity.Title.Trim() != oExistingMovie.Title.Trim())
            {
                bCreateLog = true;
            }

            if (bCreateLog)
            {
                // Create movie Log
                MoviePriceLog oLog = new MoviePriceLog();
                oLog.MovieID       = oExistingMovie.MovieID;
                oLog.Title         = oExistingMovie.Title.Trim();
                oLog.RentalPrice   = oExistingMovie.RentalPrice;
                oLog.SalePrice     = oExistingMovie.SalePrice;
                oLog.Created       = DateTime.Now;
                oLog.UpdatedUserID = oExistingMovie.UpdatedUserID;
                oLog.Updated       = DateTime.Now;
                oLogRepository.Create(oLog);
            }

            return(iResult);
        }