Пример #1
0
        public AlbumContract Create(CreateAlbumContract contract)
        {
            ParamIs.NotNull(() => contract);

            if (contract.Names == null || !contract.Names.Any())
            {
                throw new ArgumentException("Album needs at least one name", "contract");
            }

            VerifyManageDatabase();

            return(repository.HandleTransaction(ctx => {
                ctx.AuditLogger.SysLog(string.Format("creating a new album with name '{0}'", contract.Names.First().Value));

                var album = new Album {
                    DiscType = contract.DiscType
                };

                album.Names.Init(contract.Names, album);

                ctx.Save(album);

                foreach (var artistContract in contract.Artists)
                {
                    var artist = ctx.OfType <Artist>().Load(artistContract.Id);
                    if (!album.HasArtist(artist))
                    {
                        ctx.OfType <ArtistForAlbum>().Save(ctx.OfType <Artist>().Load(artist.Id).AddAlbum(album));
                    }
                }

                album.UpdateArtistString();
                var archived = Archive(ctx, album, AlbumArchiveReason.Created);
                ctx.Update(album);

                ctx.AuditLogger.AuditLog(string.Format("created album {0} ({1})", entryLinkFactory.CreateEntryLink(album), album.DiscType));
                AddEntryEditedEntry(ctx.OfType <ActivityEntry>(), album, EntryEditEvent.Created, archived);

                followedArtistNotifier.SendNotifications(ctx, album, album.ArtistList, PermissionContext.LoggedUser);

                return new AlbumContract(album, PermissionContext.LanguagePreference);
            }));
        }
Пример #2
0
        /// <summary>
        /// Updates or creates release event.
        ///
        /// Album release event names will be updated as well if the name changed.
        /// </summary>
        /// <param name="contract">Updated contract. Cannot be null.</param>
        /// <returns>Updated release event data. Cannot be null.</returns>
        /// <exception cref="DuplicateEventNameException">If the event name is already in use.</exception>
        public ReleaseEventContract Update(ReleaseEventForEditContract contract, EntryPictureFileContract pictureData)
        {
            ParamIs.NotNull(() => contract);

            PermissionContext.VerifyManageDatabase();

            return(repository.HandleTransaction(session => {
                ReleaseEvent ev;

                if (contract.Id == 0)
                {
                    var diff = new ReleaseEventDiff();

                    if (!contract.Series.IsNullOrDefault())
                    {
                        var series = session.OfType <ReleaseEventSeries>().Load(contract.Series.Id);
                        ev = new ReleaseEvent(contract.Description, contract.Date, series, contract.SeriesNumber, contract.SeriesSuffix,
                                              contract.DefaultNameLanguage, contract.CustomName);
                        series.AllEvents.Add(ev);
                    }
                    else
                    {
                        ev = new ReleaseEvent(contract.Description, contract.Date, contract.DefaultNameLanguage);
                    }

                    ev.Category = contract.Category;
                    ev.EndDate = contract.EndDate;
                    ev.SongList = session.NullSafeLoad <SongList>(contract.SongList);
                    ev.Status = contract.Status;
                    ev.VenueName = contract.VenueName;

                    if (contract.SongList != null)
                    {
                        diff.SongList.Set();
                    }

                    if (!string.IsNullOrEmpty(contract.VenueName))
                    {
                        diff.Venue.Set();
                    }

                    var weblinksDiff = WebLink.Sync(ev.WebLinks, contract.WebLinks, ev);

                    if (weblinksDiff.Changed)
                    {
                        diff.WebLinks.Set();
                    }

                    var pvDiff = ev.PVs.Sync(contract.PVs, ev.CreatePV);
                    session.OfType <PVForAlbum>().Sync(pvDiff);

                    if (pvDiff.Changed)
                    {
                        diff.PVs.Set();
                    }

                    var artistDiff = ev.SyncArtists(contract.Artists, artistId => session.Load <Artist>(artistId));

                    if (artistDiff.Changed)
                    {
                        diff.Artists.Set();
                    }

                    session.Save(ev);

                    var namesChanged = new UpdateEventNamesQuery().UpdateNames(session, ev, contract.Series, contract.CustomName, contract.SeriesNumber, contract.SeriesSuffix, contract.Names);
                    if (namesChanged)
                    {
                        session.Update(ev);
                    }

                    if (pictureData != null)
                    {
                        diff.MainPicture.Set();
                        SaveImage(ev, pictureData);
                        session.Update(ev);
                    }

                    var archived = Archive(session, ev, diff, EntryEditEvent.Created, string.Empty);
                    AddEntryEditedEntry(session.OfType <ActivityEntry>(), archived);

                    session.AuditLogger.AuditLog(string.Format("created {0}", entryLinkFactory.CreateEntryLink(ev)));

                    followedArtistNotifier.SendNotifications(session, ev, ev.Artists.Where(a => a?.Artist != null).Select(a => a.Artist), PermissionContext.LoggedUser);
                }
                else
                {
                    ev = session.Load(contract.Id);
                    permissionContext.VerifyEntryEdit(ev);

                    var diff = new ReleaseEventDiff(DoSnapshot(ev, session));

                    if (ev.Category != contract.Category)
                    {
                        diff.Category.Set();
                    }

                    if (!ev.Date.Equals(contract.Date) || !ev.EndDate.Equals(contract.EndDate))
                    {
                        diff.Date.Set();
                    }

                    if (ev.Description != contract.Description)
                    {
                        diff.Description.Set();
                    }

                    var inheritedLanguage = ev.Series == null || contract.CustomName ? contract.DefaultNameLanguage : ev.Series.TranslatedName.DefaultLanguage;
                    if (ev.TranslatedName.DefaultLanguage != inheritedLanguage)
                    {
                        diff.OriginalName.Set();
                    }

                    var namesChanged = new UpdateEventNamesQuery().UpdateNames(session, ev, contract.Series, contract.CustomName, contract.SeriesNumber, contract.SeriesSuffix, contract.Names);

                    if (namesChanged)
                    {
                        diff.Names.Set();
                    }

                    if (!ev.Series.NullSafeIdEquals(contract.Series))
                    {
                        diff.Series.Set();
                    }

                    if (ev.SeriesNumber != contract.SeriesNumber)
                    {
                        diff.SeriesNumber.Set();
                    }

                    if (ev.SeriesSuffix != contract.SeriesSuffix)
                    {
                        diff.SeriesSuffix.Set();
                    }

                    if (!ev.SongList.NullSafeIdEquals(contract.SongList))
                    {
                        diff.SongList.Set();
                    }

                    if (ev.Status != contract.Status)
                    {
                        diff.Status.Set();
                    }

                    if (!string.Equals(ev.VenueName, contract.VenueName))
                    {
                        diff.Venue.Set();
                    }

                    ev.SetSeries(session.NullSafeLoad <ReleaseEventSeries>(contract.Series));
                    ev.Category = contract.Category;
                    ev.CustomName = contract.CustomName;
                    ev.Date = contract.Date;
                    ev.Description = contract.Description;
                    ev.EndDate = contract.EndDate > contract.Date ? contract.EndDate : null;
                    ev.SeriesNumber = contract.SeriesNumber;
                    ev.SeriesSuffix = contract.SeriesSuffix;
                    ev.SongList = session.NullSafeLoad <SongList>(contract.SongList);
                    ev.Status = contract.Status;
                    ev.TranslatedName.DefaultLanguage = inheritedLanguage;
                    ev.VenueName = contract.VenueName;

                    var weblinksDiff = WebLink.Sync(ev.WebLinks, contract.WebLinks, ev);

                    if (weblinksDiff.Changed)
                    {
                        diff.WebLinks.Set();
                        session.OfType <ReleaseEventWebLink>().Sync(weblinksDiff);
                    }

                    var pvDiff = ev.PVs.Sync(contract.PVs, ev.CreatePV);
                    session.OfType <PVForAlbum>().Sync(pvDiff);

                    if (pvDiff.Changed)
                    {
                        diff.PVs.Set();
                    }

                    var artistDiff = ev.SyncArtists(contract.Artists, artistId => session.Load <Artist>(artistId));

                    if (artistDiff.Changed)
                    {
                        diff.Artists.Set();
                    }

                    if (pictureData != null)
                    {
                        diff.MainPicture.Set();
                        SaveImage(ev, pictureData);
                    }

                    session.Update(ev);

                    var archived = Archive(session, ev, diff, EntryEditEvent.Updated, string.Empty);
                    AddEntryEditedEntry(session.OfType <ActivityEntry>(), archived);

                    var logStr = string.Format("updated properties for {0} ({1})", entryLinkFactory.CreateEntryLink(ev), diff.ChangedFieldsString);
                    session.AuditLogger.AuditLog(logStr);

                    var newSongCutoff = TimeSpan.FromHours(1);
                    if (artistDiff.Added.Any() && ev.CreateDate >= DateTime.Now - newSongCutoff)
                    {
                        var addedArtists = artistDiff.Added.Where(a => a.Artist != null).Select(a => a.Artist).Distinct().ToArray();

                        if (addedArtists.Any())
                        {
                            followedArtistNotifier.SendNotifications(session, ev, addedArtists, PermissionContext.LoggedUser);
                        }
                    }
                }

                return new ReleaseEventContract(ev, LanguagePreference);
            }));
        }