コード例 #1
0
        public async Task <RedirectResult> PerformSearch(string searchTerms)
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var subjectRepository = scope.ServiceProvider.GetService <ISubjectService>();

                var exact_matches = await subjectRepository.Search(new[] { "person", "artist", "song" }, searchTerms, true, false, false);

                if (exact_matches.Count == 1)
                {
                    var    subject_type = exact_matches.First().GetType();
                    string subject_url;
                    if (subject_type == typeof(Dtos.Dtos.Person))
                    {
                        var person = exact_matches.First();
                        subject_url = spaRouteService.GenerateUrl("person-show-name", new { id = person.Id, name = person.Text.Slugify() });
                    }
                    else if (subject_type == typeof(Dtos.Dtos.Artist))
                    {
                        var artist = exact_matches.First();
                        subject_url = spaRouteService.GenerateUrl("artist-show-name", new { id = artist.Id, name = artist.Text.Slugify() });
                    }
                    else if (subject_type == typeof(Dtos.Dtos.Song))
                    {
                        var song = exact_matches.First();
                        subject_url = spaRouteService.GenerateUrl("song-show-title", new { id = song.Id, title = song.Text.Slugify() });
                    }
                    else
                    {
                        throw new Exception("The specified type is not a Subject");
                    }


                    return(new RedirectResult(subject_url));
                }
                else
                {
                    return(new RedirectResult($"/search/{searchTerms}"));
                }
            }
        }
コード例 #2
0
        public Person Get(int id)
        {
            var person = personRepository.GetPerson(id);

            //var parms = new Dictionary<string, object>();
            //parms["id"] = 5;
            var parms = new
            {
                id = 5
            };
            var route = spaRouteService.GenerateUrl("person-edit", parms);

            return(person);
        }
コード例 #3
0
        public async Task <IActionResult> Sitemap(string subject, int count, int page)
        {
            string[] languages = new string[] { "fr", "nl" };
            IEnumerable <Subject> subjects;

            switch (subject.ToLower())
            {
            case "person":
                var people = await personService.GetPeople(false, false);

                subjects = people.Skip((page - 1) * count).Take(count);
                break;

            case "artist":
                var artists = await artistService.GetArtists(false, false);

                subjects = artists.Skip((page - 1) * count).Take(count);
                break;

            case "song":
                var songs = await songService.GetSongs(false, false);

                subjects = songs.Skip((page - 1) * count).Take(count);
                break;

            default:
                return(NotFound());
            }

            return(Ok(new UrlSet(subjects.Select(s =>
            {
                switch (subject)
                {
                case "person":
                    {
                        return new Url
                        {
                            Loc = spaRouteService.GenerateUrl($"person-show-name", new { id = s.Id, name = s.Text.Slugify() }, HttpContext),
                            ChangeFreq = AspNetCore.SitemapXml.Enums.ChangeFreq.Monthly,
                            LastMod = s.DateUpdate,
                            Links = languages.Select(lang =>
                                                     new Link
                            {
                                Rel = "alternate",
                                HrefLang = lang,
                                Href = spaRouteService.GenerateUrl($"person-show-name", new { id = s.Id, name = s.Text.Slugify(), lang }, HttpContext)
                            }).ToList()
                        };
                    }

                case "artist":
                    {
                        return new Url
                        {
                            Loc = spaRouteService.GenerateUrl($"artist-show-name", new { id = s.Id, name = s.Text.Slugify() }, HttpContext),
                            ChangeFreq = AspNetCore.SitemapXml.Enums.ChangeFreq.Monthly,
                            LastMod = s.DateUpdate,
                            Links = languages.Select(lang =>
                                                     new Link
                            {
                                Rel = "alternate",
                                HrefLang = lang,
                                Href = spaRouteService.GenerateUrl($"artist-show-name", new { id = s.Id, name = s.Text.Slugify(), lang }, HttpContext)
                            }).ToList()
                        };
                    }

                case "song":
                    {
                        Func <Song, List <Video> > parseSongVideos = (song) => {
                            if (string.IsNullOrEmpty(song.YoutubeId))
                            {
                                return new List <Video>();
                            }
                            else
                            {
                                return new List <Video>
                                {
                                    new Video
                                    {
                                        Title = song.Title,
                                        Description = song.Description,
                                        ContentLocation = $"https://www.youtube.com/watch?v={song.YoutubeId}",
                                        PlayerLocation = $"https://www.youtube.com/embed/{song.YoutubeId}",
                                        ThumbnailLocation = $"http://i.ytimg.com/vi/{song.YoutubeId}/hqdefault.jpg"
                                    }
                                };
                            }
                        };
                        Func <Song, List <Image> > parseSongImages = (song) => {
                            if (string.IsNullOrEmpty(song.YoutubeId))
                            {
                                return new List <Image>();
                            }
                            else
                            {
                                return new List <Image>
                                {
                                    new Image
                                    {
                                        Title = song.Description,
                                        Caption = song.Title,
                                        Location = $"http://i.ytimg.com/vi/{song.YoutubeId}/default.jpg"
                                    }
                                };
                            }
                        };

                        return new Url
                        {
                            Loc = spaRouteService.GenerateUrl($"song-show-title", new { id = s.Id, title = s.Text.Slugify() }, HttpContext),
                            ChangeFreq = AspNetCore.SitemapXml.Enums.ChangeFreq.Monthly,
                            LastMod = s.DateUpdate,
                            Links = languages.Select(lang =>
                                                     new Link
                            {
                                Rel = "alternate",
                                HrefLang = lang,
                                Href = spaRouteService.GenerateUrl($"song-show-title", new { id = s.Id, title = s.Text.Slugify(), lang }, HttpContext)
                            }).ToList(),
                            Videos = parseSongVideos((Song)s),
                            Images = parseSongImages((Song)s)
                        };
                    }

                default:
                    throw new Exception("Unexpected subject type");
                }
            }))));
        }
        public async Task OnSupplyData(HttpContext context, IDictionary <string, object> data)
        {
            var route = await spaRouteService.GetCurrentRoute(context);

            switch (route?.Name)
            {
            case "person-list":
            {
                var people = await personService.GetPeople();

                data["people"] = people;
            }
            break;

            case "person-show":
            case "person-edit":
            {
                var personid = Convert.ToInt32(route.Parameters["personid"]);
                var person   = await personService.GetPerson(personid, false);

                if (person == null)
                {
                    context.Response.OnStarting(() =>
                        {
                            context.Response.StatusCode = StatusCodes.Status404NotFound;
                            return(Task.CompletedTask);
                        });
                }
                else
                {
                    context.Response.OnStarting(async() =>
                        {
                            var url = await spaRouteService.GenerateUrl($"{route.Name}-name", new { personid = personid, name = (person.FirstName + " " + person.LastName).Slugify() });
                            context.Response.Redirect(url);
                        });
                }
            }
            break;

            case "person-show-name":
            case "person-edit-name":
            {
                var personid = Convert.ToInt32(route.Parameters["personid"]);
                var person   = await personService.GetPerson(personid);

                if (person == null)
                {
                    context.Response.OnStarting(() =>
                        {
                            context.Response.StatusCode = StatusCodes.Status404NotFound;
                            return(Task.CompletedTask);
                        });
                }
                else if (route.Parameters["name"] == (person.FirstName + " " + person.LastName).Slugify())
                {
                    data["person"] = person;
                }
                else
                {
                    var url = await spaRouteService.GenerateUrl(route.Name, new { personid = personid, name = (person.FirstName + " " + person.LastName).Slugify() });

                    context.Response.Redirect(url);
                }
            }
            break;
            }

            data.Add("message", "Message from server");
        }