public ActionResult Create([Bind(Include = "CastMemberID,Name,YearJoined,MainRole,Bio,PhotoId,CurrentMember,CastMemberPersonId,AssociateArtist,EnsembleMember,CastYearLeft,DebutYear")] CastMember castMember, HttpPostedFileBase file)
        {
            ModelState.Remove("CastMemberPersonID");

            //Extract the Guid as type String from user's selected User (from SelectList)
            string userId = Request.Form["dbUsers"].ToString();

            // ModelState error to ensure that A user cannot be assigned to multiple cast members.
            // If the CastMemberUserID IS assigned for this user, that means that this user is assigned
            // to another Cast Member: add the ModelState error.
            if (!string.IsNullOrEmpty(userId) && db.Users.Find(userId).CastMemberUserID != 0)
            {
                ModelState.AddModelError("CastMemberPersonID", $"{db.Users.Find(userId).UserName} already has a cast member profile");
            }

            if (ModelState.IsValid)
            {
                if (file != null && file.ContentLength > 0)
                {
                    castMember.PhotoId = PhotoController.CreatePhoto(file, castMember.Name); // Call CreatePhoto method from Photocontroller and assign return value (int photo.PhotoId) to castMember.PhotoId
                }


                //ViewData["dbUsers"] = new SelectList(db.Users.ToList(), "Id", "UserName");

                if (!string.IsNullOrEmpty(userId))
                {
                    castMember.CastMemberPersonID = db.Users.Find(userId).Id;
                }
                //ModelState.Remove("PhotoId");
                db.CastMembers.Add(castMember);
                db.SaveChanges();

                // If a user was selected, update the CastMemberUserID column in the User table with CastMemberPersonID.
                if (!string.IsNullOrEmpty(userId))
                {
                    // Find the selected user.
                    var selectedUser = db.Users.Find(userId);

                    // Update the User's Cast Member Id column with castMemberId
                    selectedUser.CastMemberUserID = castMember.CastMemberID;

                    // Save the changes
                    db.Entry(selectedUser).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return(RedirectToAction("Index"));
            }
            else  // This viewdata is required for the create view
            {
                ViewData["dbUsers"] = new SelectList(db.Users.ToList(), "Id", "UserName");
            }

            return(View(castMember));
        }
Example #2
0
 public ActionResult Create([Bind(Include = "ProductionId,Title,Playwright,Description,Runtime,OpeningDay,ClosingDay,DefaultPhoto,ShowtimeEve,ShowtimeMat,TicketLink,Season,IsCurrent,IsWorldPremiere")] Production production, HttpPostedFileBase uploadFile)
 {
     //========== VALIDATION ==========
     //===== PHOTO - Check if photo is not null but not a valid photo format
     if (uploadFile != null && !PhotoController.ValidatePhoto(uploadFile))
     {
         ModelState.AddModelError("DefaultPhoto", "File must be a valid photo format.");
     }
     //===== SHOWTIME - at list one (ShowtimeEve, ShowtimeMat) need to be assigned
     if (production.ShowtimeEve == null && production.ShowtimeMat == null)
     {
         ModelState.AddModelError("ShowtimeEve", "At least one showtime must be specified.");
         ModelState.AddModelError("ShowtimeMat", "At least one showtime must be specified.");
     }
     //========== SAVE ==========
     if (ModelState.IsValid)
     {
         //===== DEFAULT PHOTO
         //--- save photo using photo controller, save entry to ProductionPhotos, photoName set to production title, description set to "Default Photo"
         if (uploadFile != null)
         {
             //----- Save New Photo or retrieve existing photo if the same - using photo controller
             Debug.WriteLine("Saving photo...");
             int photoId = PhotoController.CreatePhoto(uploadFile, production.Title);
             //----- Save New ProductionPhoto
             var productionPhoto = new ProductionPhotos()
             {
                 PhotoId = photoId, Title = production.Title, Description = "Default Photo"
             };
             db.ProductionPhotos.Add(productionPhoto);
             db.SaveChanges();
             //----- Save New Production, add DefaultPhoto object reference to production
             production.DefaultPhoto = productionPhoto;
             db.Productions.Add(production);
             db.SaveChanges();
             //----- Add Production object reference to productionPhoto
             productionPhoto.Production      = production;
             db.Entry(productionPhoto).State = EntityState.Modified;
             db.SaveChanges();
         }
         //===== NO PHOTO
         else
         {
             db.Productions.Add(production);
             db.SaveChanges();
         }
         return(RedirectToAction("Index"));
     }
     return(View(production));
 }
        public ActionResult Edit([Bind(Include = "SponsorId,Name,PhotoId,Height,Width,Current,Link")] Sponsor sponsor, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    sponsor.PhotoId = PhotoController.CreatePhoto(upload, "SponsorLogo_" + sponsor.Name);
                }

                db.Entry(sponsor).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(sponsor));
        }
        public ActionResult Create([Bind(Include = "SponsorId,Name,Height,Width,Current,Link")] Sponsor sponsor, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    //takes upload file and creates a photo.cs object from it and returns (int)photo.ID
                    sponsor.PhotoId = PhotoController.CreatePhoto(upload, "SponsorLogo_" + sponsor.Name);
                }
                db.Sponsors.Add(sponsor);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(sponsor));
        }
        //The ModelState variable only holds three key/value pairs in its dictionary after POST.
        //They are Title, Description and File (which is the file name, and should so be called.
        //Not sure whether or not Image should be in the Include parameter of the Bind method...

        public ActionResult Create([Bind(Include = "InfoId,Title,TextContent,File")] DisplayInfo displayInfo, HttpPostedFileBase file)
        {
            //byte[] image = ImageUploadController.ImageBytes(file, out string _64);
            //displayInfo.Image = image;
            //displayInfo.File = file.FileName;

            if (ModelState.IsValid)
            {
                if (file != null && file.ContentLength > 0)
                {
                    //takes upload file and creates a photo.cs object from it and returns (int)photo.ID
                    displayInfo.PhotoId = PhotoController.CreatePhoto(file, displayInfo.Title);
                }
                db.DisplayInfo.Add(displayInfo);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(displayInfo));
        }
        public ActionResult Edit([Bind(Include = "InfoId,Title,TextContent,Image, File")] DisplayInfo displayInfo, HttpPostedFileBase file)
        {
            //validates photo
            if (file != null && !PhotoController.ValidatePhoto(file))
            {
                ModelState.AddModelError("Photo", "File must be a valid photo format.");
            }

            if (ModelState.IsValid)
            {
                if (file != null && file.ContentLength > 0)
                {
                    //updates the new displayInfo's photo, and textcontent
                    displayInfo.PhotoId = PhotoController.CreatePhoto(file, "DisplayInfoPhoto_" + displayInfo.Title);
                }
                db.Entry(displayInfo).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(displayInfo));
        }
        public ActionResult AddProductionPhoto([Bind(Include = "Title,Description")] ProductionPhotos productionPhotos, HttpPostedFileBase file, int productionId)
        {
            productionPhotos.PhotoId = PhotoController.CreatePhoto(file, productionPhotos.Title);

            if (ModelState.IsValid)
            {
                Production production = db.Productions.Find(productionId);
                productionPhotos.Production = production;

                if (production.DefaultPhoto == null)
                {
                    production.DefaultPhoto = productionPhotos;
                }

                db.ProductionPhotos.Add(productionPhotos);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
            return(View("Edit"));
        }
        public ActionResult Edit([Bind(Include = "CastMemberID,Name,YearJoined,MainRole,Bio,PhotoId,CurrentMember,AssociateArtist,EnsembleMember,CastYearLeft,DebutYear")] CastMember castMember, HttpPostedFileBase file)
        {
            ModelState.Remove("CastMemberPersonID");
            string userId = Request.Form["dbUsers"].ToString();

            // ModelState error to ensure that A user cannot be assigned to multiple cast members.
            // If the userId is null, castMemberId is 0, or previous castMemberId is the same as the new CastMemberId,
            // Then don't add the model error.
            if (!string.IsNullOrEmpty(userId))
            {
                int newCastMemberId = db.Users.Find(userId).CastMemberUserID;
                if (!(newCastMemberId == 0 || castMember.CastMemberID == newCastMemberId))
                {
                    ModelState.AddModelError("CastMemberPersonID", $"{db.Users.Find(userId).UserName} already has a cast member profile");
                }
            }

            // The unmodified Cast Member to be Edited ( The 'previous' Cast Member )
            var currentCastMember = db.CastMembers.Find(castMember.CastMemberID);

            if (ModelState.IsValid)
            {
                int oldPhotoId = currentCastMember.PhotoId; // replace photo operation with photoid

                currentCastMember.Name            = castMember.Name;
                currentCastMember.YearJoined      = castMember.YearJoined;
                currentCastMember.MainRole        = castMember.MainRole;
                currentCastMember.Bio             = castMember.Bio;
                currentCastMember.CurrentMember   = castMember.CurrentMember;
                currentCastMember.AssociateArtist = castMember.AssociateArtist;
                currentCastMember.EnsembleMember  = castMember.EnsembleMember;
                currentCastMember.CastYearLeft    = castMember.CastYearLeft;
                currentCastMember.DebutYear       = castMember.DebutYear;

                string previousUserId = "";
                string newUserId      = "";

                // If the Cast Member had a previous User, get that User's Id.
                if (!string.IsNullOrEmpty(currentCastMember.CastMemberPersonID))
                {
                    previousUserId = currentCastMember.CastMemberPersonID;
                }

                // If the selected UserName is not "(No User Selected)", get that User's Id.
                if (!string.IsNullOrEmpty(userId))
                {
                    newUserId = userId;
                }

                // Only change the Cast Member's and the user's Ids if the Users changed.
                if (previousUserId != newUserId)
                {
                    Debug.WriteLine("\n\nThe Usernames changed!!\n\n");
                    // Set the previous User's CastMemberUserId to 0 if that User exists.
                    if (previousUserId != "")
                    {
                        db.Users.Find(previousUserId).CastMemberUserID = 0;
                    }

                    // Only do this if there was a User selected.  Links the Cast Member and
                    // User together by updated their associated databases.
                    if (newUserId != "")
                    {
                        // Link the Cast Member to the User
                        currentCastMember.CastMemberPersonID = userId;

                        // Get the selected User.
                        var selectedUser = db.Users.Find(userId);

                        // Update the User's Cast Member Id column with castMemberId
                        selectedUser.CastMemberUserID = castMember.CastMemberID;

                        // Save the changes
                        db.Entry(selectedUser).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    // When there is no User selected, remove the reference to the User for this cast member.
                    else
                    {
                        currentCastMember.CastMemberPersonID = null;
                    }
                }

                if (file != null && file.ContentLength > 0)
                {
                    currentCastMember.PhotoId = PhotoController.CreatePhoto(file, castMember.Name); // Call CreatePhoto method from Photocontroller and assign return value (int photo.PhotoId) to currentCastMember.PhotoId
                }
                else
                {
                    currentCastMember.PhotoId = oldPhotoId; //new attribute PhotoId
                }
                //castMember.CastMemberPersonID = db.Users.Find(userId).Id;
                //db.Entry(castMember).State = EntityState.Modified;
                db.Entry(currentCastMember).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            else
            {
                // If the ModelState is invalid for some reason, make sure to retain the Cast Member's User selection.
                //ViewData["dbUsers"] = new SelectList(db.Users.ToList(), "Id", "UserName", db.CastMembers.Find(castMember.CastMemberID).CastMemberPersonID);

                // The same thing can be acheived if I expand this line's scope: var currentCastMember = db.CastMembers.Find(castMember.CastMemberID);
                // and then use new SelectList(db.Users.ToList(), "Id", "UserName", currentCastMember.CastMemberPersonID);
                // Now that I think of it, I'm using the value of currentCastMember whether or not the ModelState is valid or not,
                // So I think I'm safe to expand it's scope.

                ViewData["dbUsers"] = new SelectList(db.Users.ToList(), "Id", "UserName", currentCastMember.CastMemberPersonID);
            }

            return(View(castMember));
        }