// AddTagTo <albumName> <tag>
        public static string Execute(string[] data)
        {
            string albumName = data[0];
            string tagName   = TagUtilities.ValidateOrTransform(data[1]);

            using (PhotoShareContext context = new PhotoShareContext())
            {
                Tag   tag   = context.Tags.SingleOrDefault(t => t.Name == tagName);
                Album album = context.Albums.SingleOrDefault(a => a.Name == albumName);

                if (tag == null || album == null)
                {
                    throw new ArgumentException("Either album or tag do not exists!");
                }

                AlbumTag albumTag = new AlbumTag()
                {
                    AlbumId = album.Id,
                    Album   = album,
                    TagId   = tag.Id,
                    Tag     = tag
                };

                if (context.AlbumTags.Any(at => at == albumTag))
                {
                    throw new InvalidOperationException($"Tag {tag.Name} is already added to album {album.Name} !");
                }

                album.AlbumTags.Add(albumTag);
                context.SaveChanges();
            }

            return($"Tag {tagName} added to {albumName}");
        }
        // CreateAlbum <username> <albumTitle> <BgColor> <tag1> <tag2>...<tagN>
        public string Execute(string[] data)
        {
            string username        = data[0];
            string albumTitle      = data[1];
            string backgroundColor = data[2];

            string[] tags = data.Skip(3).Select(t => TagUtilities.ValidateOrTransform(t)).ToArray();


            User user = GetUserByUserName(username);

            if (user == null)
            {
                throw new ArgumentException($"User {username} not found!");
            }

            Color color;
            bool  isColorValid = Enum.TryParse(backgroundColor, out color);

            if (!isColorValid)
            {
                throw new ArgumentException($"Color {color} not found!");
            }

            if (tags.Any(t => !IsTagExisting(t)))
            {
                throw new ArgumentException("Invalid tags!");
            }

            if (IsAlbumExisting(albumTitle))
            {
                throw new ArgumentException($"Albulm {albumTitle} exist!");
            }

            using (PhotoShareContext context = new PhotoShareContext())
            {
                Album album = new Album();
                album.Name            = albumTitle;
                album.BackgroundColor = color;
                album.Tags            = context.Tags.Where(t => tags.Contains(t.Name)).ToList();

                User owner = context.Users.SingleOrDefault(u => u.Username == username);

                if (owner != null)
                {
                    AlbumRole albumRole = new AlbumRole();
                    albumRole.User  = owner;
                    albumRole.Album = album;
                    albumRole.Role  = Role.Owner;
                    album.AlbumRoles.Add(albumRole);

                    context.Albums.Add(album);
                    context.SaveChanges();
                }
            }

            return($"Albulm {albumTitle} successfully created!");
        }
        //AddTag <tag>
        public override string Execute()
        {
            string tag = TagUtilities.ValidateOrTransform(Data[1]);

            this.tags.Add(new Tag
            {
                Name = tag
            });

            return(tag + " was added sucessfully to database");
        }
Exemple #4
0
        // CreateAlbum <username> <albumTitle> <BgColor> <tag1> <tag2>...<tagN>
        public string Execute(string[] data)
        {
            string username   = data[0];
            string albumTitle = data[1];
            string bgColor    = data[2];

            string[] tags = data.Skip(3).ToArray(); // there must be at least 1 tag

            // 2. Extend Photo Share System
            if (!AuthenticationService.IsAuthenticated())
            {
                throw new InvalidOperationException("Invalid credentials! You should log in first.");
            }

            if (!this.userService.IsExistingUser(username))
            {
                throw new ArgumentException($"User {username} not found!");
            }

            // 2. Extend Photo Share System
            if (!userService.HasProfileRights(username))
            {
                throw new InvalidOperationException("Invalid credentials! You can create albums only with your own profile.");
            }

            if (this.albumService.IsExistingAlbum(albumTitle))
            {
                throw new ArgumentException($"Album {albumTitle} exists!");
            }

            Color color;
            bool  isValidColor = Enum.TryParse(bgColor, out color);

            if (!isValidColor)
            {
                throw new AggregateException($"Color {bgColor} not found!");
            }

            tags = tags.Select(t => TagUtilities.ValidateOrTransform(t)).ToArray(); // validate tags

            if (tags.Any(t => !this.tagService.IsExistingTag(t)))
            {
                throw new ArgumentException("Invalid tags!");
            }

            this.albumService.AddAlbum(username, albumTitle, color, tags);

            return($"Album {albumTitle} successfully created!");
        }
        // AddTag <tag>
        public string Execute(string[] data)
        {
            Validator.ThrowExceptionIfUserIsNotLoggedIn(this.session);

            var tagName = TagUtilities.ValidateOrTransform(data[1]);

            if (this.tags.Exists(tagName))
            {
                throw new ArgumentException(string.Format(TagAlreadyExistsExceptionMessage, tagName));
            }

            this.tags.AddTag(tagName);

            return(string.Format(SuccessAddTagMessage, tagName));
        }
Exemple #6
0
        // AddTagTo <albumName> <tag>
        public static string Execute(string[] data)
        {
            if (data.Length != 3)
            {
                throw new InvalidOperationException($"Command {data[0]} not valid!");
            }

            AuthenticationCheck.CheckLogin();

            string albumTitle = data[1];
            string tagName    = TagUtilities.ValidateOrTransform(data[2]);

            using (var context = new PhotoShareContext())
            {
                var album = context.Albums
                            .Include(a => a.AlbumTags)
                            .ThenInclude(at => at.Tag)
                            .FirstOrDefault(a => a.Name.ToLower() == albumTitle.ToLower());

                var tag = context.Tags
                          .FirstOrDefault(t => t.Name.ToLower() == tagName.ToLower());

                if (album == null || tag == null)
                {
                    throw new ArgumentException("Either tag or album do not exist!");
                }

                if (album.AlbumTags.Any(at => at.Tag.Name.ToLower() == tagName.ToLower()))
                {
                    throw new ArgumentException($"Tag {tagName} already present in album {albumTitle}");
                }

                var userId = Session.User.Id;

                AuthenticationCheck.CheckAlbumOwnership(userId, album.Id, context);

                context.AlbumTags.Add(
                    new AlbumTag()
                {
                    Album = album,
                    Tag   = tag
                });

                context.SaveChanges();
            }

            return($"Tag {tagName} added to {albumTitle}!");
        }
Exemple #7
0
        public string Execute(string[] args)
        {
            string tagName = args[0];

            tagName = TagUtilities.ValidateOrTransform(tagName);

            bool tagExists = this.tagService.Exists(tagName);

            if (tagExists)
            {
                throw new ArgumentException($"Tag {tagName} already exists!");
            }

            this.tagService.AddTag(tagName);

            return($"Tag [{tagName}] was added successfully!");
        }
        // AddTag <tag>
        public string Execute(string[] data)
        {
            string tag = TagUtilities.ValidateOrTransform(data[0]);

            if (this.tagService.IsTagExisting(tag))
            {
                throw new ArgumentException($"Tag {tag} exists!");
            }

            if (!SecurityService.IsAuthenticated())
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            this.tagService.AddTag(tag);

            return($"Tag {tag} was added successfully!");
        }
Exemple #9
0
        // CreateAlbum <username> <albumTitle> <BgColor> <tag1> <tag2>...<tagN>
        public string Execute(string[] data)
        {
            string username = data[0];

            if (!this.userService.IsExistingByUsername(username))
            {
                throw new ArgumentException($"User {username} not found!");
            }

            if (!SecurityService.IsAuthenticated())
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            string albumName = data[1];

            if (SecurityService.GetCurrentUser().Username != username)
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            if (this.albumService.IsAlbumExisting(albumName))
            {
                throw new ArgumentException($"Album {albumName} exists!");
            }

            string backgroundColor = data[2];

            Color color = (Color)Enum.Parse(typeof(Color), backgroundColor);

            string[] tagsToInclude = data.Skip(3).Select(t => TagUtilities.ValidateOrTransform(t)).ToArray();

            if (tagsToInclude.Any(t => !this.tagService.IsTagExisting(t)))
            {
                throw new ArgumentException("Invalid tags!");
            }

            this.albumService.AddAlbum(username, albumName, color, tagsToInclude);

            return($"Album {albumName} successfully created!");
        }
        public string Execute(string[] data)
        {
            var albumName = data[0];
            var tagName   = data[1];

            using (PhotoShareContext context = new PhotoShareContext())
            {
                if (Session.User == null)
                {
                    throw new InvalidOperationException("Invalid credentials!");
                }

                var album = context.Albums.SingleOrDefault(a => a.Name == albumName);
                var tag   = context.Tags.SingleOrDefault(t => t.Name == TagUtilities.ValidateOrTransform(tagName));

                if (album == null || tag == null)
                {
                    throw new ArgumentException("Either tag or album do not exist!");
                }

                bool isLoggedUserOwner =
                    context
                    .AlbumRoles
                    .Any(ar => ar.UserId == Session.User.Id && ar.AlbumId == album.Id && ar.Role == Role.Owner);

                if (!isLoggedUserOwner)
                {
                    throw new InvalidOperationException("Invalid credentials!");
                }

                context.AlbumTags.Add(new AlbumTag
                {
                    Album = album,
                    Tag   = tag
                });

                context.SaveChanges();

                return($"Tag {tagName} added to {albumName}!");
            }
        }
        // AddTagTo <albumName> <tag>
        public string Execute(string[] data)
        {
            string albumName = data[0];
            string tagName   = TagUtilities.ValidateOrTransform(data[1]);

            if (!(IsAlbumExisting(albumName) || IsTagExisting(tagName)))
            {
                throw new ArgumentException("Either tag or album do not exist!");
            }

            using (PhotoShareContext context = new PhotoShareContext())
            {
                Album album = context.Albums.SingleOrDefault(a => a.Name == albumName);
                Tag   tag   = context.Tags.SingleOrDefault(t => t.Name == tagName);

                album.Tags.Add(tag);
                context.SaveChanges();
            }

            return($"Tag {tagName} added to {albumName}!");
        }
Exemple #12
0
        // CreateAlbum <username> <albumTitle> <BgColor> <tag1> <tag2>...<tagN>

        public string Execute(string[] commandParameters)
        {
            if (!AuthenticationManager.IsAuthenticated())
            {
                throw new InvalidOperationException("Log in in order to create albums!");
            }
            string username   = commandParameters[0];
            string albumTitle = commandParameters[1];
            string BgColor    = commandParameters[2];

            string[] tags = commandParameters.Skip(3).Select(t => TagUtilities.ValidateOrTransform(t)).ToArray();

            if (!this.userService.IsUsernameExisting(username))
            {
                throw new ArgumentException($"Username [{username}] not found!");
            }
            if (this.albumService.IsAlbumExisting(albumTitle))
            {
                throw new ArgumentException($"Album [{albumTitle}] is already existing!");
            }

            Color color;
            bool  isColorValid = Enum.TryParse(BgColor, out color);

            if (!isColorValid)
            {
                throw new ArgumentException($"Color [{BgColor}] not found!");
            }
            if (tags.Any(t => !this.tagService.IsTagExisting(t)))
            {
                throw new ArgumentException($"Invalid tags!");
            }

            // TODO chech album title and add to DB

            this.albumService.AddAlbum(username, albumTitle, color, tags);

            return($"Album [{albumTitle}] successfully created!");
        }
        // AddTagTo <albumName> <tag>
        public string Execute(string[] data)
        {
            string albumName = data[0];
            string tagName   = TagUtilities.ValidateOrTransform(data[1]);

            if (!this.albumService.IsAlbumExisting(albumName) || !this.tagService.IsTagExisting(tagName))
            {
                throw new ArgumentException("Either tag or album do not exist!");
            }

            if (!SecurityService.IsAuthenticated())
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            if (!this.albumService.IsUserOwnerOfAlbum(SecurityService.GetCurrentUser().Username, albumName))
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            this.tagService.AddTagTo(albumName, tagName);

            return($"Tag {tagName} added to {albumName}!");
        }
Exemple #14
0
        // AddTagTo <albumName> <tag>
        public string Execute(string[] data)
        {
            Validator.ThrowExceptionIfUserIsNotLoggedIn(this.session);

            var albumName = data[1];
            var tagName   = TagUtilities.ValidateOrTransform(data[2]);

            var album = this.albums.ByName <Album>(albumName);

            if (!album.AlbumRoles.Any(r => r.UserId == this.session.User.Id && r.Role == Role.Owner))
            {
                throw new InvalidOperationException(InvalidCredentialsExceptionMessage);
            }

            var tag = this.tags.ByName <Tag>(tagName);

            if (album == null || tag == null)
            {
                throw new ArgumentException(AlbumOrTagDoesNotExistExceptionMessage);
            }

            this.albumsTags.AddTagTo(album.Id, tag.Id);
            return(string.Format(SuccessAddTagToMessage, tag.Name, album.Name));
        }
        // CreateAlbum <username> <albumTitle> <BgColor> <tag1> <tag2>...<tagN>
        public static string Execute(string[] data)
        {
            if (data.Length < 4)
            {
                throw new InvalidOperationException($"Command {data[0]} not valid!");
            }

            AuthenticationCheck.CheckLogin();

            string username   = data[1];
            string albumTitle = data[2];
            string bgColor    = ConvertToTitleCase(data[3].ToLower());
            var    tags       = data
                                .Skip(4)
                                .Select(t => TagUtilities.ValidateOrTransform(t))
                                .ToArray();

            AuthenticationCheck.CheckUserCredentials(username);

            using (var context = new PhotoShareContext())
            {
                var user = context.Users
                           .FirstOrDefault(u => u.Username == username);

                if (user == null)
                {
                    throw new ArgumentException($"User {username} not found!");
                }

                if (context.Albums.Any(a => a.Name == albumTitle))
                {
                    throw new ArgumentException($"Album {albumTitle} exists!");
                }

                bool bgColorIsValid = Enum.TryParse(bgColor, out Color color);
                if (!bgColorIsValid)
                {
                    throw new ArgumentException($"Color {bgColor} not found!");
                }

                var album = new Album()
                {
                    Name            = albumTitle,
                    IsPublic        = false,
                    BackgroundColor = color,
                };

                context.Albums.Add(album);

                var currentAlbumRole = new AlbumRole
                {
                    Album = album,
                    User  = user,
                    Role  = Role.Owner
                };

                context.AlbumRoles.Add(currentAlbumRole);

                for (int i = 0; i < tags.Length; i++)
                {
                    var currentTag = context.Tags.FirstOrDefault(t => t.Name.ToLower() == tags[i].ToLower());

                    if (currentTag == null)
                    {
                        throw new ArgumentException("Invalid tags!");
                    }

                    context.AlbumTags.Add(
                        new AlbumTag()
                    {
                        TagId   = currentTag.Id,
                        AlbumId = album.Id
                    });
                }

                context.SaveChanges();
            }

            return($"Album {albumTitle} successfully created!");
        }
        // CreateAlbum <username> <albumTitle> <BgColor> <tag1> <tag2>...<tagN>

        public static string Execute(string[] data)
        {
            var userName      = data[0];
            var albumTitle    = data[1];
            var colorToString = data[2];
            var tags          = data.Skip(3).Select(tag => TagUtilities.ValidateOrTransform(tag)).ToArray();

            using (PhotoShareContext context = new PhotoShareContext())
            {
                User currentUser = context.Users
                                   .Where(u => u.Username == userName)
                                   .FirstOrDefault();

                if (currentUser == null)
                {
                    throw new ArgumentException($"User {userName} not found!");
                }

                if (context.Albums.SingleOrDefault(a => a.Name == albumTitle) != null)
                {
                    throw new ArgumentException($"Album {albumTitle} exists!");
                }

                if (Enum.TryParse(colorToString, out Color color))
                {
                    throw new ArgumentException($"Color {colorToString} not found!");
                }

                foreach (var tag in tags)
                {
                    if (context.Tags.SingleOrDefault(t => t.Name == tag) == null)
                    {
                        throw new ArgumentException("Invalid tags!");
                    }
                }

                Album album = new Album
                {
                    Name            = albumTitle,
                    BackgroundColor = (Color)color
                };

                context.AlbumRoles.Add(new AlbumRole
                {
                    User  = currentUser,
                    Album = album,
                    Role  = Role.Owner
                });

                foreach (var tag in tags)
                {
                    var currentTag = context.Tags.SingleOrDefault(t => t.Name == tag);

                    context.AlbumTags.Add(new AlbumTag
                    {
                        Tag   = currentTag,
                        Album = album
                    });
                }

                context.SaveChanges();

                return($"Album {albumTitle} successfully created!");
            }
        }