コード例 #1
0
        // AddTagTo <albumName> <tag>

        public string Execute(params string[] args)
        {
            string albumTitle = args[0];
            string tagName    = args[1];

            if (Session.User == null || !Session.User.AlbumRoles.Any(ar => ar.Album.Name == albumTitle))
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            if (!Checker.AlbumExists(albumTitle) || !Checker.TagsExists(tagName))
            {
                throw new ArgumentException("Either tag or album do not exist!");
            }

            using (var db = new PhotoShareContext())
            {
                Album album = db.Albums.FirstOrDefault(a => a.Name == albumTitle);
                Tag   tag   = db.Tags.FirstOrDefault(t => t.Name == tagName);

                AlbumTag albumTag = new AlbumTag()
                {
                    Album = album,
                    Tag   = tag,
                };

                album.AlbumTags.Add(albumTag);

                db.SaveChanges();
            }

            return($"Tag {tagName} added to {albumTitle}!");
        }
コード例 #2
0
        // UploadPicture <albumName> <pictureTitle> <pictureFilePath>
        public string Execute(string[] data)
        {
            string albumName    = data[0];
            string pictureTitle = data[1];
            string picturePath  = data[2];

            if (Session.User == null || Session.User.ProfilePicture.Path != picturePath)
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            if (!Checker.AlbumExists(albumName))
            {
                throw new ArgumentException($"Album {albumName} not found!");
            }

            using (var db = new PhotoShareContext())
            {
                Album album = db.Albums.FirstOrDefault(a => a.Name == albumName);

                Picture picture = new Picture()
                {
                    Path  = picturePath,
                    Title = pictureTitle,
                    Album = album,
                };

                db.Pictures.Add(picture);
                db.SaveChanges();
            }

            return($"Picture {pictureTitle} added to {albumName}!");
        }
        // ShareAlbum <albumId> <username> <permission>
        // For example:
        // ShareAlbum 4 dragon321 Owner
        // ShareAlbum 4 dragon11 Viewer
        public string Execute(params string[] args)
        {
            int    albumId    = int.Parse(args[0]);
            string username   = args[1];
            string permission = args[2];

            if (Session.User == null || Session.User.Username != username)
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            if (!Checker.AlbumExists(albumId))
            {
                throw new ArgumentException($"Album {albumId} not found!");
            }

            if (!Checker.UserExists(username))
            {
                throw new ArgumentException($"User {username} not found!");
            }

            if (!Checker.PermissionIsValid(permission))
            {
                throw new ArgumentException("Permission must be either “Owner” or “Viewer”!");
            }

            Role role = (Role)Enum.Parse(typeof(Role), permission);

            using (var db = new PhotoShareContext())
            {
                User  user  = db.Users.FirstOrDefault(u => u.Username == username);
                Album album = db.Albums.Find(albumId);

                AlbumRole albumRole = new AlbumRole()
                {
                    Album = album,
                    Role  = role,
                    User  = user,
                };

                db.AlbumRoles.Add(albumRole);
                db.SaveChanges();

                return($"Username {username} added to album {album.Name} ({permission})");
            }
        }
コード例 #4
0
        // CreateAlbum <username> <albumTitle> <BgColor> <tag1> <tag2>...<tagN>

        public string Execute(params string[] args)
        {
            string username   = args[0];
            string albumTitle = args[1];
            string bgColor    = args[2];

            string[] tagStrings = args.Skip(3).ToArray();

            if (Session.User == null || Session.User.Username != username)
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            using (var db = new PhotoShareContext())
            {
                #region Checks

                if (!Checker.UserExists(username))
                {
                    throw new ArgumentException($"User {username} not found!");
                }

                if (Checker.AlbumExists(albumTitle))
                {
                    throw new ArgumentException($"Album {albumTitle} exists!");
                }

                if (!Checker.ColorExists(bgColor))
                {
                    throw new ArgumentException($"Color {bgColor} not found!");
                }

                if (!Checker.TagsExists(tagStrings))
                {
                    throw new ArgumentException("Invalid tags!");
                }

                #endregion

                User  user  = db.Users.FirstOrDefault(u => u.Username == username);
                Color color = Enum.Parse <Color>(bgColor, true);
                Album album = new Album()
                {
                    Name = albumTitle, BackgroundColor = color
                };

                db.Albums.Add(album);
                db.SaveChanges();

                List <AlbumTag> tags = new List <AlbumTag>();

                foreach (var t in tagStrings)
                {
                    int      tagId = db.Tags.FirstOrDefault(tg => tg.Name == t).Id;
                    AlbumTag tag   = new AlbumTag()
                    {
                        AlbumId = album.Id,
                        TagId   = tagId,
                    };

                    tags.Add(tag);
                }

                album.AlbumTags = tags;

                AlbumRole albumRole = new AlbumRole()
                {
                    AlbumId = album.Id,
                    UserId  = user.Id,
                };

                user.AlbumRoles.Add(albumRole);

                db.SaveChanges();

                return($"Album {album.Name} successfully created!");
            }
        }