Exemplo n.º 1
0
        // DeleteUser <username>
        public static string Execute(string[] data)
        {
            string username = data[1];

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

                // TODO: Delete User by username (only mark him as inactive)
                if (user.IsDeleted.Value)
                {
                    throw new InvalidOperationException($"User {username} is already deleted!");
                }
                user.IsDeleted = true;
                //var userSession = user.Sessions.FirstOrDefault(s => s.LoggedOut == null);
                //userSession.LoggedOut = DateTime.Now;
                context.SaveChanges();

                return($"User {username} was deleted from the database!");
            }
        }
Exemplo n.º 2
0
        // AddTag <tag>

        public static string Execute(string[] data)
        {
            if (data.Length < 2)
            {
                throw new ArgumentException($"Command {data[0]} not valid!");
            }
            string tag = data[1].ValidateOrTransform();

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

                if (context.Tags.Any(t => t.Name == tag))
                {
                    throw new ArgumentException($"Tag {tag} exists!");
                }
                context.Tags.Add(new Tag
                {
                    Name = tag
                });

                context.SaveChanges();
            }

            return("Tag " + tag + " was added successfully!");
        }
Exemplo n.º 3
0
        // AddTown <townName> <countryName>
        public static string Execute(string[] data)
        {
            if (data.Length < 3)
            {
                throw new ArgumentException($"Command {data[0]} not valid!");
            }
            string townName = data[1];
            string country  = data[2];

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

                if (context.Towns.Any(t => t.Name == townName))
                {
                    throw new ArgumentException($"Town {townName} was already added!");
                }
                Town town = new Town
                {
                    Name    = townName,
                    Country = country
                };

                context.Towns.Add(town);
                context.SaveChanges();

                return("Town " + townName + " was added succesfully!");
            }
        }
Exemplo n.º 4
0
        // AddFriend <username1> <username2>
        public static string Execute(string[] data)
        {
            string reqUsername         = data[1];
            string addedFriendUsername = data[2];

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

                var reqUser = context.Users
                              .Include(u => u.FriendsAdded)
                              .ThenInclude(fa => fa.Friend)
                              .FirstOrDefault(u => u.Username == reqUsername);
                if (loggedUser != reqUser)
                {
                    throw new InvalidOperationException("Invalid credentials!");
                }

                if (reqUser == null)
                {
                    throw new ArgumentException($"{reqUsername} not found!");
                }
                var addedFriend = context.Users
                                  .Include(u => u.FriendsAdded)
                                  .ThenInclude(fa => fa.Friend)
                                  .FirstOrDefault(u => u.Username == addedFriendUsername);
                if (addedFriend == null)
                {
                    throw new ArgumentException($"{addedFriendUsername} not found!");
                }

                bool alreadyAdded = reqUser.FriendsAdded.Any(u => u.Friend == addedFriend);
                bool accepted     = addedFriend.FriendsAdded.Any(u => u.Friend == reqUser);
                if (alreadyAdded && accepted)
                {
                    throw new InvalidOperationException($"{addedFriendUsername} is already a friend to {reqUsername}");
                }
                if (alreadyAdded && !accepted)
                {
                    throw new InvalidOperationException($"{reqUsername} is already sent invite to {addedFriendUsername}");
                }
                if (!alreadyAdded && accepted)
                {
                    throw new InvalidOperationException($"{addedFriend} is already sent invite to {reqUser}");
                }
                reqUser.FriendsAdded.Add(
                    new Friendship()
                {
                    User   = reqUser,
                    Friend = addedFriend
                });
                context.SaveChanges();
            }
            return($"Friend {addedFriendUsername} added to {reqUsername}");
        }
Exemplo n.º 5
0
        // ShareAlbum <albumId> <username> <permission>
        // For example:
        // ShareAlbum 4 dragon321 Owner
        // ShareAlbum 4 dragon11 Viewer
        public static string Execute(string[] data)
        {
            if (data.Length < 4)
            {
                throw new ArgumentException($"Command {data[0]} not valid!");
            }
            int    albumId         = -1;
            bool   albumExist      = int.TryParse(data[1], out albumId);
            string username        = data[2];
            string inputPermission = data[3];

            Role permission;

            if (!Enum.TryParse(inputPermission, out permission))
            {
                throw new ArgumentException("Permission must be either “Owner” or “Viewer”!");
            }
            using (var context = new PhotoShareContext())
            {
                var loggedUser = IsLogged.IsLoggedIn(context);
                var user       = context.Users
                                 .Include(u => u.AlbumRoles)
                                 .FirstOrDefault(u => u.Username == username);
                if (loggedUser != user.AlbumRoles.Where(x => x.Role == Role.Owner))
                {
                    throw new InvalidOperationException("Invalid credentials!");
                }
                if (user == null)
                {
                    throw new ArgumentException($"User {username} not found!");
                }
                var album = context.Albums
                            .Include(a => a.AlbumRoles)
                            .FirstOrDefault(a => a.Id == albumId);
                if (album == null)
                {
                    throw new ArgumentException($"Album {albumId} not found!");
                }
                user.AlbumRoles.Add(new AlbumRole()
                {
                    AlbumId = albumId,
                    Role    = permission
                });
                context.SaveChanges();
            }


            return($"Username {username} added to album {albumId} ({inputPermission})");
        }
Exemplo n.º 6
0
        // UploadPicture <albumName> <pictureTitle> <pictureFilePath>
        public static string Execute(string[] data)
        {
            if (data.Length < 4)
            {
                throw new ArgumentException($"Command {data[0]} not valid!");
            }
            string albumName       = data[1];
            string pictureTitle    = data[2];
            string pictureFilePath = data[3];

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

                var album = context.Albums
                            .Include(u => u.AlbumRoles)
                            .ThenInclude(x => x.User)
                            .FirstOrDefault(a => a.Name == albumName);

                var albumOwner = album.AlbumRoles
                                 .FirstOrDefault(x => x.Role == Role.Owner).User;
                if (loggedUser != albumOwner)
                {
                    throw new InvalidOperationException("Invalid credentials!");
                }
                if (album == null)
                {
                    throw new ArgumentException($"Album {albumName} not found!");
                }
                Picture picture = new Picture()
                {
                    Path  = pictureFilePath,
                    Title = pictureTitle,
                    Album = album
                };
                album.Pictures.Add(picture);
                context.SaveChanges();
            }

            return($"Picture {pictureTitle} added to {albumName}!");
        }
Exemplo n.º 7
0
        // 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}!");
        }
Exemplo n.º 8
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}.");
            }
        }
Exemplo n.º 9
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);
        }