// AddTagTo <albumName> <tag>
        public static string Execute(string[] data)
        {
            var albumName = data[1];
            var tag       = data[2];

            using (var context = new PhotoShareContext())
            {
                var loggedUser = IsLogged.IsLoggedIn(context);

                Album album = context.Albums
                              .Include(a => a.AlbumRoles)
                              .ThenInclude(ar => ar.User)
                              .FirstOrDefault(a => a.Name == albumName);
                User albumOwner = album.AlbumRoles
                                  .FirstOrDefault(x => x.Role == Role.Owner)
                                  .User;
                if (loggedUser != albumOwner)
                {
                    throw new InvalidOperationException("Invalid credentials!");
                }
                Tag tagToAdd = context.Tags.FirstOrDefault(t => t.Name == "#" + tag);
                if (album == null || tagToAdd == null)
                {
                    throw new ArgumentException($"Either {tag} or {albumName} do not exist!");
                }

                AlbumTag albumTag = new AlbumTag()
                {
                    Album = album,
                    Tag   = tagToAdd
                };
                context.AlbumTags.Add(albumTag);
                context.SaveChanges();
            }
            return($"Tag {tag} added to {albumName}!");
        }
Example #2
0
        // ModifyUser <username> <property> <new value>
        // For example:
        // ModifyUser <username> Password <NewPassword>
        // ModifyUser <username> BornTown <newBornTownName>
        // ModifyUser <username> CurrentTown <newCurrentTownName>
        // !!! Cannot change username
        public static string Execute(string[] data)
        {
            string username = data[1];
            string property = data[2].ToLower();
            string newValue = data[3];

            var exceptionMessage = $"Value {newValue} not valid." + Environment.NewLine;
            var errorTown        = $"Town {newValue} not found!";

            using (var context = new Data.PhotoShareContext())
            {
                User loggedUser = IsLogged.IsLoggedIn(context);

                User user = context.Users
                            .Where(u => u.Username == username)
                            .FirstOrDefault();
                if (loggedUser != user)
                {
                    throw new InvalidOperationException("Invalid credentials!");
                }
                if (user == null)
                {
                    throw new ArgumentException($"User {username} not found!");
                }
                switch (property)
                {
                case "password":
                    if (newValue.Length < 6 ||
                        newValue.Length > 50 ||
                        !newValue.Any(c => Char.IsDigit(c)) ||
                        !newValue.Any(c => Char.IsLower(c)))
                    {
                        throw new ArgumentException(exceptionMessage + "Invalid password");
                    }
                    user.Password = newValue;
                    break;

                case "borntown":
                    var bornTown = context.Towns
                                   .Where(t => t.Name == newValue)
                                   .FirstOrDefault();
                    if (bornTown == null)
                    {
                        throw new ArgumentException(exceptionMessage + errorTown);
                    }
                    user.BornTown = bornTown;
                    break;

                case "currenttown":
                    var currentown = context.Towns
                                     .Where(t => t.Name == newValue)
                                     .FirstOrDefault();
                    if (currentown == null)
                    {
                        throw new ArgumentException(exceptionMessage + errorTown);
                    }
                    user.CurrentTown = currentown;
                    break;

                default:
                    throw new ArgumentException($"Property {property} not supported!");
                }
                context.SaveChanges();
                return($"User {username} {property} is {newValue}.");
            }
        }
Example #3
0
        // CreateAlbum <username> <albumTitle> <BgColor> <tag1> <tag2>...<tagN>
        public static string Execute(string[] data)
        {
            string username   = data[1];
            string albumTitle = data[2];

            if (data.Length < 3)
            {
                throw new ArgumentException($"Color {String.Empty} not found!");
            }
            string color = data[3];

            string[] tags   = data.Skip(4).ToArray();
            var      result = "";

            using (var context = new PhotoShareContext())
            {
                var loggedUser   = IsLogged.IsLoggedIn(context);
                var existingUser = context.Users
                                   .FirstOrDefault(u => u.Username == username);
                if (loggedUser != existingUser)
                {
                    throw new InvalidOperationException("Invalid credentials!");
                }
                if (existingUser == null)
                {
                    throw new ArgumentException($"User {username} not found!");
                }

                if (context.Albums.Any(a => a.Name == albumTitle))
                {
                    throw new ArgumentException($"Album {albumTitle} exists!");
                }
                Album album = new Album()
                {
                    Name = albumTitle
                };
                try
                {
                    Color colorValue = (Color)Enum.Parse(typeof(Color), color);
                    album.BackgroundColor = colorValue;
                    context.Albums.Add(album);

                    AlbumRole albumRole = new AlbumRole()
                    {
                        User  = existingUser,
                        Album = album,
                        Role  = Role.Owner
                    };
                    context.AlbumRoles.Add(albumRole);
                }
                catch (ArgumentException)
                {
                    throw new ArgumentException($"Color {color} not found!");
                }
                foreach (var tag in tags)
                {
                    var tagExists = context.Tags
                                    .Where(t => t.Name == "#" + tag)
                                    .FirstOrDefault();
                    if (tagExists == null)
                    {
                        throw new ArgumentException($"Invalid tags!");
                    }
                    AlbumTag albumTag = new AlbumTag()
                    {
                        Album = album,
                        Tag   = tagExists
                    };
                    context.AlbumTags.Add(albumTag);
                }
                context.SaveChanges();
                result = $"Album {albumTitle} successfully created!";
            }

            return(result);
        }