Esempio n. 1
0
        private void SetUserRoles(EditUserViewModel model, ApplicationUser user, GalleryDbContext db)
        {
            var userManager = Request
                              .GetOwinContext()
                              .GetUserManager <ApplicationUserManager>();

            foreach (var role in model.Roles)
            {
                if (role.IsSelected)
                {
                    userManager.AddToRole(user.Id, role.Name);
                }
                else
                {
                    userManager.RemoveFromRole(user.Id, role.Name);
                }
            }
        }
Esempio n. 2
0
        public void PhotoRemove()
        {
            var detail = new PhotoDetailModel()
            {
                Name = "RemoveTestPhoto"
            };

            var photo = photoRepositorySUT.Insert(detail);

            Assert.NotNull(photo);

            photoRepositorySUT.Remove(photo.Id);

            using (var context = new GalleryDbContext())
            {
                Assert.DoesNotContain(context.Photos, x => x.Id == photo.Id);
            }
        }
        //
        //GET: Category
        public ActionResult Index(string sortOrder, string searchedCategory, string currentFilter, int?page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.Name        = string.IsNullOrEmpty(sortOrder) ? "Name_desc" : string.Empty;

            if (searchedCategory != null)
            {
                page = 1;
            }
            else
            {
                searchedCategory = currentFilter;
            }

            ViewBag.CurrentFilter = searchedCategory;

            using (var db = new GalleryDbContext())
            {
                var categories = db.Categories.ToList();

                if (!string.IsNullOrEmpty(searchedCategory))
                {
                    categories = categories.Where(c => c.Name.ToLower().Contains(searchedCategory.ToLower())).ToList();

                    if (categories.Count == 0)
                    {
                        this.AddNotification("No categories containing this string were found..", NotificationType.INFO);
                    }
                }

                switch (sortOrder)
                {
                case "Name_desc":
                    categories = categories.OrderByDescending(s => s.Name).ToList();
                    break;
                }

                int pageSize   = 10;
                int pageNumber = (page ?? 1);

                return(View(categories.ToPagedList(pageNumber, pageSize)));
            }
        }
Esempio n. 4
0
        public ActionResult ShowProfile(string id)
        {
            if (id == null)
            {
                this.AddNotification("No user ID provided.", NotificationType.ERROR);
                return(RedirectToAction("Index", "Home"));
            }

            var model = new ProfileViewModel();

            using (var db = new GalleryDbContext())
            {
                var user = db.Users.FirstOrDefault(u => u.Id == id);

                if (user == null)
                {
                    this.AddNotification("Such a user doesn't exist.", NotificationType.ERROR);
                    return(RedirectToAction("Index", "Home"));
                }

                model.Id            = user.Id;
                model.FullName      = user.FirstName + " " + user.LastName;
                model.Email         = user.Email;
                model.IsEmailPublic = user.IsEmailPublic;
                model.Gender        = user.Gender;
                model.City          = user.City;
                model.Country       = user.Country;
                model.Birthday      = user.Birthday;
                model.PhoneNumber   = user.PhoneNumber;

                model.ImagePath = string.IsNullOrEmpty(user.ImagePath)
                    ? "~/Content/images/blank-profile-picture.png"
                    : user.ImagePath;

                model.SampleUserPics = db.Pictures.
                                       Where(p => p.PicUploaderId == user.Id)
                                       .OrderByDescending(p => p.Id)
                                       .Take(6)
                                       .ToList();
            }

            return(View(model));
        }
        public ActionResult RatePicture(FormCollection form)
        {
            var rating    = int.Parse(form["Rating"]);
            var pictureId = int.Parse(form["PictureId"]);

            if (rating != 0)
            {
                using (var db = new GalleryDbContext())
                {
                    var picture = db.Pictures.FirstOrDefault(p => p.Id == pictureId);

                    if (picture == null)
                    {
                        this.AddNotification("Such a picture doesn't exist.", NotificationType.ERROR);
                        return(RedirectToAction("ListCategories", "Home"));
                    }

                    picture.RatingSum += rating;
                    picture.RatingCount++;
                    picture.Rating = picture.RatingSum / picture.RatingCount;

                    // Getting the ID of the currently logged in user
                    var currentUserId = User.Identity.GetUserId();

                    // Adding the ID to the IDs of the users that have already rated this picture
                    picture.UserIdsRatedPic += " " + currentUserId;

                    db.Entry(picture).State = EntityState.Modified;
                    db.SaveChanges();

                    if (rating == 1)
                    {
                        this.AddNotification("The picture was rated with 1 star.", NotificationType.SUCCESS);
                    }
                    else
                    {
                        this.AddNotification($"The picture was rated with {rating} stars.", NotificationType.SUCCESS);
                    }
                }
            }

            return(RedirectToAction("Details", new { id = pictureId }));
        }
        public ActionResult DeleteConfirmed(int?id)
        {
            if (id == null)
            {
                this.AddNotification("No picture ID provided.", NotificationType.ERROR);
                return(RedirectToAction("ListCategories", "Home"));
            }

            using (var db = new GalleryDbContext())
            {
                // Get picture from database
                var picture = db.Pictures
                              .Where(p => p.Id == id)
                              .Include(p => p.PicUploader)
                              .Include(p => p.Category)
                              .FirstOrDefault();

                // Check if picture exists
                if (picture == null)
                {
                    this.AddNotification("Such a picture doesn't exist.", NotificationType.ERROR);
                    return(RedirectToAction("ListCategories", "Home"));
                }

                // Adjust the positional boolean variables of the other pics before the deletion
                AdjustCategoryPositions.Delete(db, picture);

                // Getting the category of the pic before the deletion in order to redirect to Home/ListPictures after that
                var picCategoryId = (int?)picture.CategoryId;

                // Delete the picture from the ~/Content/images/astroPics folder:
                var physicalPath = Server.MapPath(picture.ImagePath);
                System.IO.File.Delete(physicalPath);

                // Delete the picture from the database
                db.Pictures.Remove(picture);
                db.SaveChanges();

                // Redirect to the page with all pics in the current category
                this.AddNotification("The picture was deleted.", NotificationType.SUCCESS);
                return(RedirectToAction("ListPictures", "Home", new { id = picCategoryId }));
            }
        }
Esempio n. 7
0
        protected override void Seed(GalleryDbContext context)
        {
            if (!context.Roles.Any())
            {
                this.CreateRole(context, "Admin");
                this.CreateRole(context, "User");
            }

            if (!context.Users.Any())
            {
                this.CreateUser(context, "*****@*****.**", "Astrogallery", "Administrator", "password1");
                this.SetRoleToUser(context, "*****@*****.**", "Admin");
            }

            if (!context.Categories.Any())
            {
                this.CreateCategory(context, "Other");
            }
        }
Esempio n. 8
0
        public void AlbumRemove()
        {
            var detail = new AlbumDetailModel()
            {
                Id     = Guid.NewGuid(),
                Name   = "TestovaciAlbum",
                Photos = new List <PhotoEntity>()
            };
            var album = albumRepositorySUT.Insert(detail);

            Assert.NotNull(album);

            albumRepositorySUT.Remove(detail.Id);

            using (var context = new GalleryDbContext())
            {
                Assert.DoesNotContain(context.Albums, x => x.Id == detail.Id);
            }
        }
Esempio n. 9
0
        public void RemoveObject(Guid id)
        {
            using (var galleryDbContext = new GalleryDbContext())
            {
                var entity = galleryDbContext.Objects.First(p => p.ObjectId == id);

                galleryDbContext.TagSubjects.Attach(entity);

                galleryDbContext.TagSubjects.Remove(entity);

                try
                {
                    galleryDbContext.SaveChanges();
                }
                catch (Exception e)
                {
                    return;
                }
            }
        }
Esempio n. 10
0
 public UnitOfWork(GalleryDbContext context, IRepository <Artist> artistrepository, IRepository <City> cityrepo, IRepository <Country> countryrepo,
                   IRepository <CurrentExhibition> currexhrepo, IRepository <Employee> employeerepo, IRepository <ExhibitedPicture> exhpicrepo,
                   IRepository <Exhibition> exibitionrepo, IRepository <ExhibitPlace> exhplacerepo, IRepository <OwnedPicture> ownpicrepo,
                   IRepository <Owner> ownerrepo, IRepository <Picture> picturerepo, IRepository <Technique> techniquerepo, IRepository <TicketsInCart> ticketsInCartRepository)
 {
     _context                    = context;
     ArtistRepository            = artistrepository;
     CityRepository              = cityrepo;
     CountryRepository           = countryrepo;
     CurrentExhibitionRepository = currexhrepo;
     EmployeeRepository          = employeerepo;
     ExhibitedPictureRepository  = exhpicrepo;
     ExhibitionRepository        = exibitionrepo;
     ExhibitPlaceRepository      = exhplacerepo;
     OwnedPictureRepository      = ownpicrepo;
     OwnerRepository             = ownerrepo;
     PictureRepository           = picturerepo;
     TechniqueRepository         = techniquerepo;
     TicketsInCartRepository     = ticketsInCartRepository;
 }
Esempio n. 11
0
        public void PersonRemove()
        {
            var thing = new ThingsListModel()
            {
                Name = "Obraz"
            };
            var inserted = thingRepositorySUT.Insert(thing);

            Assert.NotNull(inserted);
            using (var context = new GalleryDbContext())
            {
                Assert.Contains(context.Things, x => x.Id == inserted.Id);
            }

            thingRepositorySUT.Remove(inserted.Id);

            using (var context = new GalleryDbContext())
            {
                Assert.DoesNotContain(context.Things, x => x.Id == inserted.Id);
            }
        }
Esempio n. 12
0
        public void Remove(Guid id)
        {
            using (var galleryDbContext = new GalleryDbContext())
            {
                var itemToRemove = galleryDbContext.Albums.SingleOrDefault(x => x.Id == id);
                if (itemToRemove != null)
                {
                    var photosWithThisAlbum = galleryDbContext.Photos.Where(x => x.Album.Id == itemToRemove.Id)
                                              .ToList();
                    foreach (var p in photosWithThisAlbum)
                    {
                        p.Album = null;
                    }

                    galleryDbContext.SaveChanges();

                    galleryDbContext.Albums.Remove(itemToRemove);
                    galleryDbContext.SaveChanges();
                }
            }
        }
        //
        //GET: Category/Delete/id
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                this.AddNotification("No category ID provided.", NotificationType.ERROR);
                return(RedirectToAction("Index"));
            }

            using (var db = new GalleryDbContext())
            {
                var category = db.Categories.FirstOrDefault(c => c.Id == id);

                if (category == null)
                {
                    this.AddNotification("Such a category doesn't exist.", NotificationType.ERROR);
                    return(RedirectToAction("Index"));
                }

                return(View(category));
            }
        }
Esempio n. 14
0
        public void PersonRemove()
        {
            var person = new PersonListModel()
            {
                Firstname = "Michaela",
                Surname   = "Nova"
            };
            var inserted = personRepositorySUT.Insert(person);

            Assert.NotNull(inserted);
            using (var context = new GalleryDbContext())
            {
                Assert.Contains(context.Persons, x => x.Id == inserted.Id);
            }

            personRepositorySUT.Remove(inserted.Id);

            using (var context = new GalleryDbContext())
            {
                Assert.DoesNotContain(context.Persons, x => x.Id == inserted.Id);
            }
        }
Esempio n. 15
0
        public ActionResult DeleteImages(IEnumerable <int> ImagesIDs)
        {
            using (GalleryDbContext db = new GalleryDbContext())
            {
                foreach (var id in ImagesIDs)
                {
                    // Retrieve an image
                    var image = db.gallery.Single(s => s.ID == id);

                    // Delete the image from the server and database
                    string imgPath = Server.MapPath(image.ImagePath);
                    db.gallery.Remove(image);

                    if (System.IO.File.Exists(imgPath))
                    {
                        System.IO.File.Delete(imgPath);
                    }
                }
                db.SaveChanges();
            }
            return(RedirectToAction("DeleteImages"));
        }
        public ActionResult DeleteConfirmed(int?id)
        {
            if (id == null)
            {
                this.AddNotification("No category ID provided.", NotificationType.ERROR);
                return(RedirectToAction("Index"));
            }

            using (var db = new GalleryDbContext())
            {
                var category = db.Categories.FirstOrDefault(c => c.Id == id);

                if (category == null)
                {
                    this.AddNotification("Such a category doesn't exist.", NotificationType.ERROR);
                    return(RedirectToAction("Index"));
                }

                // When a category is being deleted all the pictures in that category in DB must be deleted:
                var picsToBeDeleted = category.Pictures.ToList();

                foreach (var pic in picsToBeDeleted)
                {
                    // Delete the pic from DB
                    db.Pictures.Remove(pic);
                }

                // Delete the category's directory + all files in it(recursive true)
                Directory.Delete(Server.MapPath($"~/Content/images/astroPics/{category.Name}"), true);

                db.Categories.Remove(category);
                db.SaveChanges();

                this.AddNotification("Category deleted.", NotificationType.SUCCESS);

                return(RedirectToAction("Index"));
            }
        }
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                this.AddNotification("No picture ID provided.", NotificationType.ERROR);
                return(RedirectToAction("ListCategories", "Home"));
            }

            using (var db = new GalleryDbContext())
            {
                // Get picture from database
                var picture = db.Pictures
                              .Where(p => p.Id == id)
                              .Include(p => p.PicUploader)
                              .Include(p => p.Category)
                              .FirstOrDefault();

                // Check if picture exists
                if (picture == null)
                {
                    this.AddNotification("Such a picture doesn't exist.", NotificationType.ERROR);
                    return(RedirectToAction("ListCategories", "Home"));
                }

                ViewBag.TagsString = string.Join(" ", picture.Tags.Select(t => t.Name));

                if (!IsUserAuthorizedToEditAndDelete(picture))
                {
                    this.AddNotification("You don't have the necessary authority to delete this picture.", NotificationType.ERROR);

                    return(RedirectToAction("Details", new { id = picture.Id }));
                }

                // Pass picture to the view
                return(View(picture));
            }
        }
Esempio n. 18
0
        public void PhotoInsert_WithAlbum()
        {
            AlbumEntity album;

            using (var context = new GalleryDbContext())
            {
                album = context.Albums.First();
            }

            var detail = new PhotoDetailModel()
            {
                Name  = "AlbumTestPhoto",
                Album = album
            };

            var photo = photoRepositorySUT.Insert(detail);

            Assert.NotNull(photo);

            using (var context = new GalleryDbContext())
            {
                Assert.Contains(context.Photos, x => x.Id == photo.Id);
            }
        }
Esempio n. 19
0
        //
        // GET: Tag/ListPicsWithTag/id
        public ActionResult ListPicsWithTag(int?id, int?page)
        {
            if (id == null)
            {
                this.AddNotification("No tag ID provided.", NotificationType.ERROR);
                return(RedirectToAction("Index", "Home"));
            }

            using (var db = new GalleryDbContext())
            {
                var requestedTag = db.Tags
                                   .Include(t => t.Pictures.Select(p => p.Tags))
                                   .Include(t => t.Pictures.Select(p => p.PicUploader))
                                   .FirstOrDefault(t => t.Id == id);

                if (requestedTag == null)
                {
                    this.AddNotification("Such a tag does not exist.", NotificationType.ERROR);
                    return(RedirectToAction("Index", "Home"));
                }

                //Get pictures from db
                var pictures = db.Tags
                               .Include(t => t.Pictures.Select(p => p.Tags))
                               .Include(t => t.Pictures.Select(p => p.PicUploader))
                               .FirstOrDefault(t => t.Id == id)
                               .Pictures
                               .OrderByDescending(p => p.Id)
                               .ToList();

                //Return the view
                int pageSize   = 8;
                int pageNumber = (page ?? 1);
                return(View(pictures.ToPagedList(pageNumber, pageSize)));
            }
        }
Esempio n. 20
0
        //
        //GET: User/Delete/id
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                this.AddNotification("No user ID provided.", NotificationType.ERROR);
                return(RedirectToAction("Index"));
            }

            using (var db = new GalleryDbContext())
            {
                //Get user from database
                var user = db.Users
                           .FirstOrDefault(u => u.Id == id);

                //Check if user exists
                if (user == null)
                {
                    this.AddNotification("Such a user doesn't exist.", NotificationType.ERROR);
                    return(RedirectToAction("Index"));
                }

                return(View(user));
            }
        }
Esempio n. 21
0
 public ItemService(GalleryDbContext db)
 {
     this.db = db;
 }
Esempio n. 22
0
        public ActionResult Edit(EditViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var postedProfilePic = Request.Files["image"];

            var pic          = Path.GetFileName(postedProfilePic.FileName);
            var hasNewImage  = false;
            var userIdFolder = model.Id;

            // If a picture is selected AND the remove picture option is not selected:
            if (!string.IsNullOrEmpty(pic) && !model.RemovePicture)
            {
                var userDirectory = Server.MapPath($"~/Content/images/profilePics/{userIdFolder}");

                if (!Directory.Exists(userDirectory))
                {
                    Directory.CreateDirectory(userDirectory);
                }

                var path = Path.Combine(Server.MapPath($"~/Content/images/profilePics/{userIdFolder}/"), pic);
                postedProfilePic.SaveAs(path);

                if (ImageValidator.IsImageValid(path))
                {
                    hasNewImage = true;
                }
                else
                {
                    // Deleting the file from ~/Content/images/profilePics:
                    System.IO.File.Delete(path);

                    // If the person does not have a previous profile picture his/her whole folder is deleted
                    if (!Directory.EnumerateFiles(Server.MapPath($"~/Content/images/profilePics/{userIdFolder}")).Any())
                    {
                        Directory.Delete(Server.MapPath($"~/Content/images/profilePics/{userIdFolder}"), true);
                    }

                    this.AddNotification("Invalid picture format.", NotificationType.ERROR);
                    return(View(model));
                }
            }

            var id = User.Identity.GetUserId();

            using (var db = new GalleryDbContext())
            {
                var user = db.Users.FirstOrDefault(u => u.Id == id);

                if (user == null)
                {
                    this.AddNotification("Such a user doesn't exist.", NotificationType.ERROR);
                    return(RedirectToAction("Index", "Home"));
                }

                user.FirstName   = model.FirstName;
                user.LastName    = model.LastName;
                user.PhoneNumber = model.PhoneNumber;
                user.Gender      = model.Gender;
                user.City        = model.City;
                user.Country     = model.Country;

                if (string.IsNullOrEmpty(model.Birthday))
                {
                    user.Birthday = string.Empty;
                }
                else
                {
                    user.Birthday = user.Birthday;
                }

                if (model.RemoveBirthday)
                {
                    user.Birthday = string.Empty;
                }
                else
                {
                    user.Birthday = model.Birthday;
                }

                if (model.RemovePicture)
                {
                    // Delete the user's directory and the pic in there from ~/Content/images/profilePics:
                    Directory.Delete(Server.MapPath($"~/Content/images/profilePics/{userIdFolder}"), true);

                    hasNewImage    = false;
                    user.ImagePath = null;
                }

                if (hasNewImage)
                {
                    // Deleting the previous image from ~/Content/images/profilePics:
                    if (user.ImagePath != null)
                    {
                        var previousPicPath = Server.MapPath(user.ImagePath);
                        System.IO.File.Delete(previousPicPath);
                    }

                    user.ImagePath = $"~/Content/images/profilePics/{userIdFolder}/" + pic;
                }

                if (model.IsEmailPublic)
                {
                    user.IsEmailPublic = true;
                }
                else
                {
                    user.IsEmailPublic = false;
                }

                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
            }

            this.AddNotification("Profile edited.", NotificationType.SUCCESS);
            return(RedirectToAction("MyProfile", "Account"));
        }
Esempio n. 23
0
 public PicturesAdminController(GalleryDbContext context)
 {
     _context = context;
 }
Esempio n. 24
0
 public GalleryService(GalleryDbContext context)
 {
     _context = context;
 }
Esempio n. 25
0
 public ImageService(GalleryDbContext ctx)
 {
     _ctx = ctx;
 }
Esempio n. 26
0
 public ArtistsAdminController(GalleryDbContext context)
 {
     _context = context;
 }
 public GallerySqlRepository(GalleryDbContext context)
 {
     _context = context;
 }
Esempio n. 28
0
 public GalleryService(GalleryDbContext db, ConnectDbContext connectDb)
 {
     _db        = db;
     _connectDb = connectDb;
 }
        public ActionResult Upload(PictureViewModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                using (var db = new GalleryDbContext())
                {
                    // Get uploader's ID
                    var uploaderId = db.Users
                                     .First(u => u.UserName == User.Identity.Name)
                                     .Id;

                    var picture = new Picture(uploaderId, model.PicTitle, model.PicDescription, model.CategoryId);

                    // Getting the name of the category to add it to the current picture's property:
                    var picCategoryName = db.Categories
                                          .Where(c => c.Id == picture.CategoryId)
                                          .Select(c => c.Name)
                                          .ToArray();

                    picture.CategoryName = picCategoryName[0];

                    SetPictureTags(picture, model, db);

                    if (image != null && image.ContentLength > 0)
                    {
                        var imagesOfThatCategoryDir = $"~/Content/images/astroPics/{picture.CategoryName}/";
                        var picFileName             = image.FileName;
                        var uploadPath   = imagesOfThatCategoryDir + picFileName;
                        var physicalPath = Server.MapPath(uploadPath);

                        // In case the picture already exists a notification is shown:
                        if (System.IO.File.Exists(physicalPath))
                        {
                            this.AddNotification("Picture with this file name already exists in that category.", NotificationType.ERROR);

                            model.Categories = db.Categories
                                               .OrderBy(c => c.Name)
                                               .ToList();

                            return(View(model));
                        }

                        image.SaveAs(physicalPath);

                        if (ImageValidator.IsImageValid(physicalPath))
                        {
                            picture.ImagePath = uploadPath;

                            AdjustCategoryPositions.Upload(db, picture);

                            return(RedirectToAction("Details", new { id = picture.Id }));
                        }
                        else
                        {
                            // Deleting the file from ~/Content/images/astroPics/:
                            System.IO.File.Delete(physicalPath);

                            this.AddNotification("Invalid picture format.", NotificationType.ERROR);

                            model.Categories = db.Categories
                                               .OrderBy(c => c.Name)
                                               .ToList();

                            return(View(model));
                        }
                    }
                    else
                    {
                        this.AddNotification("No picture selected for upload or empty file chosen.", NotificationType.ERROR);

                        model.Categories = db.Categories
                                           .OrderBy(c => c.Name)
                                           .ToList();

                        return(View(model));
                    }
                }
            }

            using (var db = new GalleryDbContext())
            {
                model.Categories = db.Categories
                                   .OrderBy(c => c.Name)
                                   .ToList();

                return(View(model));
            }
        }
 public CategoryService(GalleryDbContext dbContext)
 {
     this._dbContext = dbContext;
 }