public LinkModel Execute(CreateLinkArgument argument)
        {
            var domain = _domainRepository.GetDomain(argument.Link.DomainId);

            var code = argument.Link.Code;

            if (!String.IsNullOrEmpty(code))
            {
                if (!LinkHelper.IsValidLinkCode(_storageService, domain.Name, argument.Link.Code))
                {
                    throw new ArgumentException($"Shortlink {domain.Name}/{argument.Link.Code} is already in use.");
                }
            }
            else
            {
                code = LinkHelper.GetUniqueLinkShortCode(_storageService, domain.Name);
            }

            Repository.Entities.Link dbLink;

            switch (argument.Link.MediaType)
            {
            case MediaType.Music:
                var uniqMediaServiceIds =
                    argument.MusicDestinations.SelectMany(x => x.Value.Select(d => d.MediaServiceId)).Distinct().ToList();
                var mediaServices = _mediaServiceRepository.GetMediaServices().Where(x => uniqMediaServiceIds.Contains(x.Id)).ToList();

                var    shortLink       = LinkHelper.ShortLinkTemplate(domain.Name, code);
                string generalLinkPath = LinkHelper.LinkGeneralFilenameTemplate(shortLink);

                dbLink = _linkRepository.CreateLink(new Repository.Entities.Link()
                {
                    Code      = code,
                    Domain    = domain,
                    DomainId  = domain.Id,
                    Id        = argument.Link.Id,
                    IsActive  = argument.Link.IsActive,
                    MediaType = argument.Link.MediaType,
                    Title     = argument.Link.Title,
                    Url       = argument.Link.Url,
                    Artists   = argument.Link.Artists?.Any() == true
                            ? argument.Link.Artists.Select(x => new Artist()
                    {
                        Id    = x.Id,
                        Name  = x.Name,
                        Label = x.Label
                    }).ToList()
                            : null
                });

                _storageService.Save(generalLinkPath, new StorageModel()
                {
                    Id           = argument.Link.Id,
                    MediaType    = argument.Link.MediaType,
                    Url          = argument.Link.Url,
                    Title        = argument.Link.Title,
                    Destinations = argument.MusicDestinations.ToDictionary(
                        md => md.Key,
                        md => md.Value.Where(d => mediaServices.Select(m => m.Id).Contains(d.MediaServiceId)).Select(d => new DestinationStorageModel()
                    {
                        MediaServiceId = d.MediaServiceId,
                        TrackingInfo   = new TrackingStorageModel()
                        {
                            MediaServiceName = mediaServices.First(m => m.Id == d.MediaServiceId).Name,

                            Artist    = d.TrackingInfo?.Artist,
                            Album     = d.TrackingInfo?.Album,
                            SongTitle = d.TrackingInfo?.SongTitle,

                            Mobile = d.TrackingInfo?.Mobile,
                            Web    = d.TrackingInfo?.Web,
                        }
                    }).ToList())
                });

                break;

            case MediaType.Ticket:

                var    shortLinkTicket       = LinkHelper.ShortLinkTemplate(domain.Name, code);
                string generalLinkPathTicket = LinkHelper.LinkGeneralFilenameTemplate(shortLinkTicket);

                dbLink = _linkRepository.CreateLink(new Repository.Entities.Link()
                {
                    Id        = argument.Link.Id,
                    Code      = code,
                    Domain    = domain,
                    DomainId  = domain.Id,
                    IsActive  = true,
                    MediaType = argument.Link.MediaType,
                    Title     = argument.Link.Title,
                    Url       = argument.Link.Url,
                    Artists   = argument.Link.Artists?.Any() == true
                            ? argument.Link.Artists.Select(x => new Artist()
                    {
                        Id    = x.Id,
                        Name  = x.Name,
                        Label = x.Label
                    }).ToList()
                            : null
                });

                _storageService.Save(generalLinkPathTicket, new Models.StorageModel.Ticket.StorageModel()
                {
                    Id           = argument.Link.Id,
                    MediaType    = argument.Link.MediaType,
                    Url          = argument.Link.Url,
                    Title        = argument.Link.Title,
                    Destinations = argument.TicketDestinations.ToDictionary(
                        md => md.Key,
                        md => md.Value.Select(d => new Models.StorageModel.Ticket.DestinationStorageModel()
                    {
                        ShowId         = d.ShowId,
                        MediaServiceId = d.MediaServiceId,
                        Url            = d.Url,
                        Date           = d.Date,
                        Location       = d.Location,
                        Venue          = d.Venue
                    }).ToList())
                });
                break;

            default:
                throw new NotSupportedException($"Link type {argument.Link.MediaType} is not supported.");
            }
            return(LinkHelper.GenerateLinkModel(dbLink));
        }
Beispiel #2
0
        public LinkModel Execute(DeleteLinkArgument argument)
        {
            var dbLink = _linkRepository.DeleteLink(argument.LinkId);

            return(LinkHelper.GenerateLinkModel(dbLink));
        }