예제 #1
0
        /// <author>Lars Toft Jacobsen</author>
        /// <summary>
        /// Publish new media
        /// </summary>
        /// <param name="info"></param>
        /// <param name="credentials"></param>
        /// <returns></returns>
        public int PublishMedia(MediaInfo info, AccountCredentials credentials)
        {
            Account account = ValidateCredentials(credentials);
            if (account == null)
            {
                throw new FaultException<InvalidCredentialsException>(
                    new InvalidCredentialsException("Invalid credentials submitted.")); ;
            }

            if (!Util.IsPublisher(account))
                throw new FaultException<InvalidCredentialsException>(
                    new InvalidCredentialsException("This user is not a publisher."));

            if (info.Price < 0)
            {
                throw new FaultException<InvalidCredentialsException>(
                    new InvalidCredentialsException("The price of the media cannot be negative."));
            }

            var db = new DatabaseDataContext();
            Genre genre;

            // fetch mediatype and genre id's
            if (!db.Media_types.Exists(t => t.name.Equals(info.Type)))
            {
                throw new FaultException<ArgumentException>(
                    new ArgumentException("Invalid media type parameter"));
            }
            Media_type mtype = (from t in db.Media_types
                                where t.name.Equals(info.Type)
                                select t).Single();

            // Add the genre if it doesn't already exist.
            int genreId = Util.AddGenre(info.Genre, info.Type);

            genre = (from g in db.Genres
                     where g.id == genreId
                     select g).Single();

            // Check if the specified publisher exists.
            if (!db.Publishers.Exists(p => p.title.Equals(info.Publisher)))
            {
                throw new FaultException<ArgumentException>(
                    new ArgumentException("Invalid publisher parameter"));
            }

            Publisher publisher = (from p in db.Publishers
                                   where p.title.Equals(info.Publisher)
                                   select p).Single();

            try
            {
                var newMedia = new RentItDatabase.Media
                {
                    title = info.Title,
                    genre_id = genre.id,
                    type_id = mtype.id,
                    price = info.Price,
                    release_date = info.ReleaseDate,
                    publisher_id = publisher.id,
                    active = true
                };

                switch (info.Type)
                {
                    case MediaType.Album:
                        AlbumInfo albumInfo = (AlbumInfo)info;
                        RentItDatabase.Album newAlbum = new Album()
                        {
                            Media = newMedia,
                            album_artist = albumInfo.AlbumArtist,
                            description = albumInfo.Description
                        };
                        db.Albums.InsertOnSubmit(newAlbum);
                        break;
                    case MediaType.Book:
                        BookInfo bookInfo = (BookInfo)info;
                        RentItDatabase.Book newBook = new Book()
                        {
                            Media = newMedia,
                            author = bookInfo.Author,
                            pages = bookInfo.Pages,
                            summary = bookInfo.Summary
                        };
                        db.Books.InsertOnSubmit(newBook);
                        break;
                    case MediaType.Movie:
                        MovieInfo movieInfo = (MovieInfo)info;
                        RentItDatabase.Movie newMovie = new Movie()
                        {
                            Media = newMedia,
                            director = movieInfo.Director,
                            length = (int)movieInfo.Duration.TotalSeconds,
                            summary = movieInfo.Summary
                        };
                        db.Movies.InsertOnSubmit(newMovie);
                        break;
                    case MediaType.Song:
                        SongInfo songInfo = (SongInfo)info;
                        RentItDatabase.Song newSong = new Song()
                        {
                            Media = newMedia,
                            artist = songInfo.Artist,
                            length = (int)songInfo.Duration.TotalSeconds
                        };

                        db.Songs.InsertOnSubmit(newSong);

                        RentItDatabase.Album_song albumSong = new Album_song()
                        {
                            album_id = songInfo.AlbumId,
                            Song = newSong
                        };

                        db.Album_songs.InsertOnSubmit(albumSong);
                        break;
                }

                db.Medias.InsertOnSubmit(newMedia);
                db.SubmitChanges();

                var newRating = new RentItDatabase.Rating
                {
                    media_id = newMedia.id,
                    avg_rating = 0.0,
                    ratings_count = 0
                };
                db.Ratings.InsertOnSubmit(newRating);
                db.SubmitChanges();

                return newMedia.id;
            }
            catch (Exception e)
            {
                throw new FaultException<Exception>(
                    new Exception("An internal error has occured. This is not related to the input: " + e.Message));
            }
        }
예제 #2
0
 partial void UpdateBook(Book instance);
예제 #3
0
 partial void DeleteBook(Book instance);
예제 #4
0
 partial void InsertBook(Book instance);