partial void DeleteMedia(Media instance);
/// <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)); } }
partial void InsertMedia(Media instance);
partial void UpdateMedia(Media instance);
private void detach_Medias(Media entity) { this.SendPropertyChanging(); entity.Publisher = null; }
private void attach_Medias(Media entity) { this.SendPropertyChanging(); entity.Publisher = this; }
private void detach_Medias(Media entity) { this.SendPropertyChanging(); entity.Media_type = null; }
private void attach_Medias(Media entity) { this.SendPropertyChanging(); entity.Media_type = this; }
/// <author>Kenneth Søhrmann</author> /// <summary> /// Returns a string, which is a concatenation of the metadata stored in the database /// about a media. Utilized to easily apply search text the a media item. /// </summary> /// <param name="mediaItem"> /// The media item to get the metadata of. /// </param> /// <returns> /// The metadata of the media, concatenated to a single string. /// </returns> public static string GetMediaMetadataAsString(Media mediaItem) { var metadataString = new StringBuilder(); metadataString.Append(mediaItem.title + " "); metadataString.Append(mediaItem.Publisher.title); switch (MediaTypeOfValue(mediaItem.Media_type.name)) { case MediaType.Book: RentItDatabase.Book book = mediaItem.Book; metadataString.Append(book.author + " "); metadataString.Append(book.summary); return metadataString.ToString(); case MediaType.Movie: RentItDatabase.Movie movie = mediaItem.Movie; metadataString.Append(movie.director + " "); metadataString.Append(movie.summary); return metadataString.ToString(); case MediaType.Song: RentItDatabase.Song song = mediaItem.Song; metadataString.Append(song.artist + " "); return metadataString.ToString(); case MediaType.Album: RentItDatabase.Album album = mediaItem.Album; metadataString.Append(album.album_artist + " "); metadataString.Append(album.description); return metadataString.ToString(); default: return metadataString.ToString(); } }