Beispiel #1
0
        public AlbumEntityModel GetAlbum(int id)
        {
            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                /*
                 *  the error with the disposed thingy is because i missed including the user (lazy loading)
                 *  which caused the app to crash, because when i try to access user properties later on, the user
                 *  for an album is not loaded, and the connection is closed by then
                 */
                AlbumEntityModel entity = cx.Albums
                                          .Where(a => a.Id == id)
                                          .Include(a => a.Photos)
                                          .Include(a => a.Comments)
                                          .Include(a => a.User)
                                          .FirstOrDefault();

                return(entity);
            }


            //using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
            //{
            //    UserEntityModel user = _context.Users
            //        .Where(u => u.Id == IserModelId)
            //        .Include(u => u.Albums
            //            .Select(p => p.Photos
            //            .Select(c => c.Comments))) //neccessary to include the albums
            //        .FirstOrDefault();
            //    return user;
            //}
        }
Beispiel #2
0
 public List <AlbumEntityModel> GetAll()
 {
     using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
     {
         return(_context.Albums.ToList());
     }
 }
        public ActionResult PhotoCreate(PhotoUploadViewModel model, HttpPostedFileBase[] photofiles, AlbumListedViewModel albumidmodel /*albumid*/)
        {
            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                AlbumEntityModel entity = cx.Albums.FirstOrDefault(a => a.Id == albumidmodel.Id);

                foreach (var file in photofiles)
                {
                    //Create new photo entity foreach file that gets uploaded through the UI
                    PhotoEntityModel uploadedPhoto = new PhotoEntityModel()
                    {
                        Name        = model.Name,
                        FileName    = file.FileName,
                        Description = model.Description,
                    };


                    //save physical file representation of photo
                    file.SaveAs(Server.MapPath($"~/photos/{uploadedPhoto.FileName}"));

                    //save class object representation of photo into album we are currently in
                    entity.Photos.Add(uploadedPhoto);

                    //persist/save to database
                    cx.SaveChanges();
                }
            }

            System.Threading.Thread.Sleep(800);//simulate waiting time

            return(RedirectToAction("Dashboard", "Account"));
        }
Beispiel #4
0
        public ActionResult AlbumDetails(int id)
        {
            AlbumDetailsViewModel model = null;

            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                var entity = cx.Albums
                             .Include(a => a.Photos)
                             .Include(a => a.Comments)
                             .FirstOrDefault(a => a.Id == id);

                model = new AlbumDetailsViewModel()
                {
                    Id          = entity.Id,
                    Name        = entity.Name,
                    Comments    = entity.Comments,
                    DateCreated = entity.DateCreated,
                    Description = entity.Description,
                    Photos      = entity.Photos,
                    User        = entity.User,
                };
            }

            return(View(model));
        }
Beispiel #5
0
        public ActionResult Dashboard()
        {
            #region retrieving the claim values for the currently logged in user (to retrieve him using his/her id)
            ClaimsIdentity currentIdentity = User.Identity as ClaimsIdentity;
            string         modelfullname   = (currentIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)).ToString();      //not used
            string         modelusername   = (currentIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName)).ToString(); //not used
            var            modelid         = int.Parse((currentIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)).Value);
            #endregion

            UserDetailsViewModel model = null;

            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                UserEntityModel entity = cx.Users.FirstOrDefault(u => u.Id == modelid);

                #region mapping necessary properties from entitymodel to the viewmodel
                model = new UserDetailsViewModel()
                {
                    Id             = entity.Id,
                    Username       = entity.Username,
                    Fullname       = entity.Fullname,
                    Albums         = entity.Albums,
                    DateRegistered = entity.DateRegistered,
                    Email          = entity.Email,
                };
                #endregion
            }

            return(View(model));
        }
Beispiel #6
0
 public AlbumEntityModel Get(int albumId)
 {
     using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
     {
         return(_context.Albums.FirstOrDefault(a => a.Id == albumId));
     }
 }
Beispiel #7
0
 public UserEntityModel RetrieveLoggedInUser(string username, string password)
 {
     using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
     {
         return(_context.Users
                .FirstOrDefault(u => u.Username == username &&
                                u.Password == password));
     }
 }
Beispiel #8
0
        public void Add(UserEntityModel userToBeRegistered)
        {
            using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
            {
                _context.Users.Add(userToBeRegistered);

                _context.SaveChanges();
            }
        }
Beispiel #9
0
        public List <PhotoEntityModel> RetrieveAll()
        {
            using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
            {
                List <PhotoEntityModel> allPhotosFromDB = _context.Photos.ToList();

                return(allPhotosFromDB);
            }
        }
Beispiel #10
0
        public ActionResult PhotoDeletePartial(int Id)
        {
            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                //delete photo with mathcing id
                cx.Photos.Remove(cx.Photos.FirstOrDefault(p => p.Id == Id));
                cx.SaveChanges();
            }

            return(RedirectToAction("index", "Home"));
        }
Beispiel #11
0
        public void NewAlbumComment(int albumid, CommentEntityModel newAlbumComment)
        {
            using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
            {
                var albumEntity = _context.Albums.FirstOrDefault(a => a.Id == albumid);

                albumEntity.Comments.Add(newAlbumComment);

                _context.SaveChanges();
            }
        }
Beispiel #12
0
 public PhotoEntityModel GetPhoto(int id)
 {
     using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
     {
         return(_context.Photos
                .Include(p => p.User)
                .Include(p => p.Album)
                .Include(p => p.Comments)
                .FirstOrDefault(p => p.Id == id));
     }
 }
Beispiel #13
0
 public UserEntityModel GetUser(int IserModelId)
 {
     using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
     {
         UserEntityModel user = _context.Users
                                .Where(u => u.Id == IserModelId)
                                .Include(u => u.Albums
                                         .Select(p => p.Photos
                                                 .Select(c => c.Comments))) //neccessary to include the albums
                                .FirstOrDefault();
         return(user);
     }
 }
Beispiel #14
0
        public List <UserEntityModel> RetrieveAllUsers()
        {
            List <UserEntityModel> entities = new List <UserEntityModel>();

            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                entities = cx.Users
                           .Include(u => u.Albums
                                    .Select(a => a.Photos))
                           .ToList();
            }

            return(entities);
        }
Beispiel #15
0
        public ActionResult PhotoDetails(int Id)
        {
            //todo: retrieve this photos uploader
            #region retrieve photo to show
            PhotoDetailsViewModel model = null;

            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                PhotoEntityModel entity = cx.Photos
                                          .Where(p => p.Id == Id)
                                          .FirstOrDefault();

                ///todo: make this more effective..
                #region retrieve uploader of photoentity
                UserEntityModel photoOwnerEntity = null;
                foreach (var user in cx.Users)
                {
                    foreach (var album in user.Albums)
                    {
                        foreach (var photo in album.Photos)
                        {
                            if (photo.Id == Id)
                            {
                                photoOwnerEntity = user;
                                break;
                            }
                        }
                    }
                }
                #endregion


                model = new PhotoDetailsViewModel()
                {
                    Id          = entity.Id,
                    Name        = entity.Name,
                    FileName    = entity.FileName,
                    DateCreated = entity.DateCreated,
                    DateChanged = entity.DateChanged,
                    Album       = entity.Album,
                    Comments    = entity.Comments, //due to us already having the model collection initialized in the photodetailsviewmodel class, we only have to transfer the collection VALUES from the entity collection to the model collection.
                    Description = entity.Description,
                    User        = photoOwnerEntity,
                };
            }
            #endregion

            return(View(model));
        }
Beispiel #16
0
        public void UpdatePhoto(int id, string photoName, string photoDescription)
        {
            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                //retrieve photo and update with new info
                PhotoEntityModel entity = cx.Photos.FirstOrDefault(p => p.Id == id);

                entity.Name = photoName;

                entity.Description = photoDescription;

                entity.DateChanged = DateTime.Now;

                cx.SaveChanges();
            }
        }
Beispiel #17
0
        public ActionResult Comment(int id, string txt_comment)
        {
            PhotoDetailsViewModel model = null;

            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                // retrieve currently logged in user
                ClaimsIdentity  currentIdentity = User.Identity as ClaimsIdentity;
                int             userid          = int.Parse(currentIdentity.Claims.FirstOrDefault(u => u.Type == ClaimTypes.NameIdentifier).Value);
                UserEntityModel loggedInEntity  = cx.Users.FirstOrDefault(u => u.Id == userid);

                // initialize new comment entity
                CommentEntityModel commentModel = new CommentEntityModel()
                {
                    DateCreated = DateTime.Now,
                    Comment     = txt_comment,
                    Commenter   = loggedInEntity.Username,
                };

                // retrieve the photo entity commented on
                PhotoEntityModel entity = cx.Photos
                                          .Where(p => p.Id == id)
                                          .FirstOrDefault();

                entity.Comments.Add(commentModel);

                cx.SaveChanges();

                //mapping
                model = new PhotoDetailsViewModel()
                {
                    Name        = entity.Name,
                    Album       = entity.Album,
                    DateCreated = entity.DateCreated,
                    FileName    = entity.FileName,
                    Description = entity.Description,
                    Id          = entity.Id,
                    User        = loggedInEntity,
                    Comments    = entity.Comments,
                };
            }

            /*
             *  note: only updating portion of the page by using partial view (with the NEW model)
             */
            return(PartialView("_PhotoComments", model));
        }
Beispiel #18
0
        public void RegisterNewUser(string fullname, string username, string password, string email)
        {
            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                UserEntityModel entity = new UserEntityModel()
                {
                    Fullname = fullname,
                    Username = username,
                    Password = password,
                    Email    = email,
                };

                cx.Users.Add(entity);

                cx.SaveChanges();
            }
        }
Beispiel #19
0
        public void CreateAlbum(int userid, string albumName, string albumDescription)
        {
            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                AlbumEntityModel newEntityAlbum = new AlbumEntityModel()
                {
                    Name        = albumName,
                    Description = albumDescription,
                };

                var userEntity = cx.Users.FirstOrDefault(u => u.Id == userid);

                userEntity.Albums.Add(newEntityAlbum);

                cx.SaveChanges();
            }
        }
        public ActionResult Login(UserLoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                var userToLogin = new UserEntityModel();

                #region get user to login
                using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
                {
                    userToLogin = cx.Users
                                  .FirstOrDefault(u => u.Username == model.Username &&
                                                  u.Password == model.Password);
                }
                #endregion

                if (userToLogin != null)
                {
                    #region when user is found, set the necessary userloginviewmodel properties
                    model.Id       = userToLogin.Id;
                    model.Fullname = userToLogin.Fullname;
                    model.Username = userToLogin.Username;
                    model.Password = userToLogin.Password;
                    #endregion

                    var identity = new ClaimsIdentity(new[]
                    {
                        //A claim is a statement that one subject makes about itself or another subject.
                        new Claim(ClaimTypes.Name, model.Fullname),
                        new Claim(ClaimTypes.GivenName, model.Username),
                        new Claim(ClaimTypes.NameIdentifier, model.Id.ToString()),
                    }, "ApplicationCookie");

                    // IOwinContext wraps OWIN environment dictionary and provides strongly typed accessors.
                    IOwinContext owinCtx = HttpContext.GetOwinContext();

                    // IAuthenticationManager is used to interact with authentication middleware that have been chained in the pipeline.
                    IAuthenticationManager authManager = owinCtx.Authentication;

                    authManager.SignIn(identity);

                    return(RedirectToAction("Index", "Home"));
                }
            }
            return(View());//we stay on the login page if login fails
        }
Beispiel #21
0
        public void DeletePhoto(int id)
        {
            //using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
            //{
            //    PhotoEntityModel photoToBeRemoved = _context.Photos
            //        .FirstOrDefault(p => p.Id == photo.Id);

            //    _context.Photos.Remove(photoToBeRemoved);

            //    _context.SaveChanges();
            //}

            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                cx.Photos.Remove(cx.Photos.FirstOrDefault(p => p.Id == id));

                cx.SaveChanges();
            }
        }
Beispiel #22
0
        public ActionResult PhotoEdit(PhotoEditViewModel model, int id)
        {
            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                //retrieve photo and update with new info
                PhotoEntityModel entity = cx.Photos.FirstOrDefault(p => p.Id == id);

                entity.Name = model.Name;

                entity.Description = model.Description;

                entity.DateChanged = DateTime.Now;

                cx.SaveChanges();
            }

            System.Threading.Thread.Sleep(800);//simulate waiting time

            return(RedirectToAction("Dashboard"));
        }
Beispiel #23
0
        public ActionResult AlbumCreate(AlbumCreateViewModel model)
        {
            ClaimsIdentity currentIdentity = User.Identity as ClaimsIdentity;
            int            userid          = int.Parse(currentIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);

            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                AlbumEntityModel newEntityAlbum = new AlbumEntityModel()
                {
                    Name        = model.Name,
                    Description = model.Description,
                };

                var userEntity = cx.Users.FirstOrDefault(u => u.Id == userid);

                userEntity.Albums.Add(newEntityAlbum);

                cx.SaveChanges();
            }

            System.Threading.Thread.Sleep(500);//simulate waiting time

            return(RedirectToAction("Dashboard", "Account"));
        }
Beispiel #24
0
        public AlbumEntityModel Add(AlbumEntityModel newAlbum, int userId)
        {
            using (PhotoExplorerEntities _context = new PhotoExplorerEntities())
            {
                //get the owner of the album
                var albumUser = _context.Users.Where(u => u.Id == userId)
                                .FirstOrDefault();

                //set some properties of the new album
                //newAlbum.AlbumId = Guid.NewGuid();
                newAlbum.DateCreated = DateTime.Now;
                newAlbum.Photos      = new List <PhotoEntityModel>();
                newAlbum.User        = albumUser;
                newAlbum.Comments    = new List <CommentEntityModel>();

                //add the album to the users albums
                albumUser.Albums.Add(newAlbum);
                //_context.Albums.Add(newAlbum);

                _context.SaveChanges();

                return(newAlbum);
            }
        }
Beispiel #25
0
        private void Seed()
        {
            /*
             *  due to us not adding the user,album and comments properties to the instantiation
             *  of the photoentities, we cannot include them later on for the photos when retrieving them
             *  int he photodetails action in contentmanagementcontroller. we have to get the specific
             *  photo by going through the users and their albums, and then the photos.
             */
            #region user 1
            PhotoEntityModel p1 = new PhotoEntityModel()
            {
                FileName    = "event1.jpg",
                Name        = "first event",
                DateCreated = DateTime.Now,
                Description = "no description",
            };
            PhotoEntityModel p2 = new PhotoEntityModel()
            {
                FileName    = "event2.jpg",
                Name        = "second event",
                DateCreated = DateTime.Now,
                Description = "no description",
            };
            PhotoEntityModel p3 = new PhotoEntityModel()
            {
                FileName    = "event3.jpg",
                Name        = "third event",
                DateCreated = DateTime.Now,
                Description = "no description",
            };

            AlbumEntityModel a1 = new AlbumEntityModel()
            {
                Name        = "my coding events",
                Description = "coding events that i have attended",
                DateCreated = DateTime.Now,
                Comments    = new List <CommentEntityModel>(),
                Photos      = new List <PhotoEntityModel>()
                {
                    p1, p2, p3
                },
            };

            UserEntityModel u1 = new UserEntityModel()
            {
                Fullname       = "Ivan Prgomet",
                Username       = "******",
                Email          = "*****@*****.**",
                Password       = "******",
                DateRegistered = DateTime.Now,
                Albums         = new List <AlbumEntityModel>()
                {
                    a1
                },
            };
            #endregion

            #region user 2
            PhotoEntityModel p4 = new PhotoEntityModel()
            {
                FileName    = "code1.jpg",
                Name        = "some code",
                DateCreated = DateTime.Now,
                Description = "no description",
            };
            AlbumEntityModel a2 = new AlbumEntityModel()
            {
                Name        = "random coding photos",
                Description = "just some coding casual coding photos",
                DateCreated = DateTime.Now,
                Comments    = new List <CommentEntityModel>(),
                Photos      = new List <PhotoEntityModel>()
                {
                    p4
                },
            };

            UserEntityModel u2 = new UserEntityModel()
            {
                Fullname       = "lea winchester",
                Username       = "******",
                Email          = "*****@*****.**",
                Password       = "******",
                DateRegistered = DateTime.Now,
                Albums         = new List <AlbumEntityModel>()
                {
                    a2
                },
            };
            #endregion

            #region user 3
            PhotoEntityModel p5 = new PhotoEntityModel()
            {
                FileName    = "mockerie1.jpg",
                Name        = "mock one",
                DateCreated = DateTime.Now,
                Description = "no description",
            };
            PhotoEntityModel p6 = new PhotoEntityModel()
            {
                FileName    = "mockerie2.jpg",
                Name        = "mock two",
                DateCreated = DateTime.Now,
                Description = "no description",
            };
            PhotoEntityModel p7 = new PhotoEntityModel()
            {
                FileName    = "mockerie3.jpg",
                Name        = "mock three",
                DateCreated = DateTime.Now,
                Description = "no description",
            };
            PhotoEntityModel p8 = new PhotoEntityModel()
            {
                FileName    = "mockerie4.jpg",
                Name        = "mock four",
                DateCreated = DateTime.Now,
                Description = "no description",
            };
            AlbumEntityModel a3 = new AlbumEntityModel()
            {
                Name        = "mockups",
                Description = "some of my experimental mockups",
                DateCreated = DateTime.Now,
                Comments    = new List <CommentEntityModel>(),
                Photos      = new List <PhotoEntityModel>()
                {
                    p5, p6, p7, p8
                },
            };

            UserEntityModel u3 = new UserEntityModel()
            {
                Fullname       = "jason bourne",
                Username       = "******",
                Email          = "*****@*****.**",
                Password       = "******",
                DateRegistered = DateTime.Now,
                Albums         = new List <AlbumEntityModel>()
                {
                    a3
                },
            };
            #endregion
            using (PhotoExplorerEntities context = new PhotoExplorerEntities())
            {
                context.Users.AddRange(new List <UserEntityModel> {
                    u1, u2, u3
                });

                context.SaveChanges();
            }
        }
Beispiel #26
0
        public ActionResult Details(int Id)
        {
            PhotoDetailsViewModel model = EFMapper.EntityToModel(photoRepo.GetPhoto(Id));

            using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            {
                foreach (var user in cx.Users)
                {
                    foreach (var album in user.Albums)
                    {
                        foreach (var photo in album.Photos)
                        {
                            if (photo.Id == Id)
                            {
                                model.User = user;
                                break;
                            }
                        }
                    }
                }
            }

            #region notused
            ////todo: retrieve this photos uploader
            //#region retrieve photo to show
            //PhotoDetailsViewModel model = null;

            //using (PhotoExplorerEntities cx = new PhotoExplorerEntities())
            //{
            //    PhotoEntityModel entity = cx.Photos
            //        .Where(p => p.Id == Id)
            //        .FirstOrDefault();

            //    ///todo: make this more effective..
            //    #region retrieve uploader of photoentity
            //    UserEntityModel photoOwnerEntity = null;
            //    foreach (var user in cx.Users)
            //    {
            //        foreach (var album in user.Albums)
            //        {
            //            foreach (var photo in album.Photos)
            //            {
            //                if (photo.Id == Id)
            //                {
            //                    photoOwnerEntity = user;
            //                    break;
            //                }
            //            }
            //        }
            //    }
            //    #endregion


            //    model = new PhotoDetailsViewModel()
            //    {
            //        Id = entity.Id,
            //        Name = entity.Name,
            //        FileName = entity.FileName,
            //        DateCreated = entity.DateCreated,
            //        DateChanged = entity.DateChanged,
            //        Album = entity.Album,
            //        Comments = entity.Comments, //due to us already having the model collection initialized in the photodetailsviewmodel class, we only have to transfer the collection VALUES from the entity collection to the model collection.
            //        Description = entity.Description,
            //        User = photoOwnerEntity,
            //    };
            //}
            //#endregion
            #endregion

            return(View("Details", model));
        }