Пример #1
0
        public int CreateAlbum(AlbumCreationViewModel album)
        {
            int albumId;

            var label       = _organizationLabelRepository.GetById(album.PublisherLabelId);
            var isrcSeries  = _organizationIsrcSeriesRepository.GetById(album.Publisher.IsrcSeriesId);
            var currentDate = DateTime.Now;

            var productPackage = new media_product_package
            {
                albumtitle           = album.BasicInfo.AlbumTitle,
                albumid              = 0,
                physicallocation     = "",
                labelid              = album.PublisherLabelId,
                cataloguenumber      = "",
                releasetypecode      = "0",
                countryofproduction  = label.countrycode,
                countryofpublication = label.countrycode,
                releasedate          = currentDate,
                packagestatusid      = 4,
                numberoftracks       = album.BasicInfo.NumberOfTracks,
                formattypeid         = 2,
                comment              = album.ReviewComment,
                updatedby            = "User",
                updatedon            = currentDate,
                createdby            = "User",
                createdon            = currentDate,
                mainartistid         = album.BasicInfo.MainArtistId
            };

            _albumRepository.Add(productPackage);

            _unitOfWork.Commit();

            albumId = productPackage.id;

            foreach (var song in album.Songs)
            {
                // If newly created song has already been created with the same isrc.
                if (song.Id == -1 && _mediaRecordingRepository.Get(m => m.isrc == song.Isrc) != null)
                {
                    throw new DuplicateNameException("Isrc provided does already exist.");
                }

                int recordingId;

                if (song.Id == -1)
                {
                    //   1.3. Create media_recording (s)
                    var recording = new media_recording
                    {
                        isrc                 = song.Isrc,
                        recordingtitle       = song.Name,
                        workingtitle         = song.Name,
                        recordingcountrycode = label.countrycode,
                        statusid             = 4,
                        updatedby            = "User",
                        updatedon            = currentDate,
                        createdby            = "User",
                        createdon            = currentDate,
                        recordingdate        = song.RecordingDate,
                        duration             = song.Length,
                        mainartist           = album.BasicInfo.MainArtistId,
                        markedfordeletion    = false
                    };

                    _mediaRecordingRepository.Add(recording);
                    _unitOfWork.Commit();

                    recordingId = recording.id;
                }
                else
                {
                    recordingId = song.Id;
                }

                //   1.4. Create media_product (s)
                _songRepository.Add(new media_product
                {
                    isrc                = song.Isrc,
                    recordingid         = recordingId,
                    title               = song.Name,
                    tracknumber         = song.Number,
                    sidenumber          = 1,
                    labelid             = album.PublisherLabelId,
                    cataloguenumber     = "",
                    mediaproducttypeid  = 1,
                    packageid           = albumId,
                    releasedate         = currentDate,
                    countryofproduction = label.countrycode,
                    statusid            = 4,
                    updatedby           = "User",
                    updatedon           = currentDate,
                    createdby           = "User",
                    createdon           = currentDate,
                    is_deleted          = false
                });

                //   1.5. Add to recording_party
                song.Performers.ForEach(performer => _recordingPartyRepository.Add(new recording_party
                {
                    recordingid    = recordingId,
                    partyrealid    = performer.Id,
                    rolecode       = performer.Role.RoleCode,
                    instrumentcode = performer.Instrument.IdCode,
                    updatedby      = "User",
                    updatedon      = currentDate,
                    createdby      = "User",
                    createdon      = currentDate,
                    status         = 2
                }));
            }

            isrcSeries.updatedon            = currentDate;
            isrcSeries.updatedby            = "User";
            isrcSeries.isrc_lastusednumber += 100;
            isrcSeries.isrc_lastusedyear    = DateTime.Now.Year;

            _organizationIsrcSeriesRepository.Update(isrcSeries);

            //   1.6. Commit changes
            _unitOfWork.Commit();

            return(albumId);
        }