This repository was archived by the owner on Dec 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathRandomController.cs
More file actions
124 lines (113 loc) · 3.96 KB
/
Copy pathRandomController.cs
File metadata and controls
124 lines (113 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Commons;
using Commons.Collections;
using Commons.Enums;
using Commons.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace WebAPI.Controllers
{
/// <summary>
/// R Controller for random resources
/// </summary>
[ApiVersion("1")]
[Route("random")]
[ApiController]
public class RandomController : Controller
{
private readonly ILogger<RandomController> _logger;
private AnimeCollection _animeCollection = new AnimeCollection();
private AnimeSongCollection _animeSongCollection = new AnimeSongCollection();
public RandomController(ILogger<RandomController> logger)
{
_logger = logger;
}
/// <summary>
/// Retrieves a list of random Anime
/// </summary>
[AllowAnonymous]
[EnableCors("CorsEveryone")]
[HttpGet("anime"), MapToApiVersion("1")]
[HttpGet("anime/{count}"), MapToApiVersion("1")]
[HttpGet("anime/{count}/{nsfw}"), MapToApiVersion("1")]
public APIResponse RandomAnime(int count = 1, bool nsfw = false, AnimeFormatEnum? format = null)
{
try
{
var query = this._animeCollection.Collection
.AsQueryable();
if (!nsfw)
{
query = query.Where(x =>
!x.Genres.Contains("Hentai") &&
!x.Genres.Contains("Nudity") &&
!x.Genres.Contains("Ecchi") &&
!x.Genres.Contains("Drugs") &&
!x.Genres.Contains("Rape") &&
!x.Genres.Contains("Handjob"));
}
if (format.HasValue)
{
query = query.Where(x => x.Format == format.Value);
}
List<Anime> anime = query.Sample(count > 50 ? 50 : count).ToList();
if (anime == null || anime.Count == 0)
{
throw new APIException(HttpStatusCode.NotFound,
"Zero anime found",
new List<Anime>());
}
return APIManager.SuccessResponse($"{anime.Count} random anime found", anime);
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
/// <summary>
/// Retrieves a list of random Song
/// </summary>
[AllowAnonymous]
[EnableCors("CorsEveryone")]
[HttpGet("song"), MapToApiVersion("1")]
[HttpGet("song/{count}"), MapToApiVersion("1")]
public APIResponse RandomSong(int count = 1)
{
try
{
List<AnimeSong> animeSongs = this._animeSongCollection.Collection
.AsQueryable()
.Sample(count > 50 ? 50 : count)
.ToList();
if (animeSongs == null || animeSongs.Count == 0)
{
throw new APIException(HttpStatusCode.NotFound,
"Zero songs found",
new List<AnimeSong>());
}
return APIManager.SuccessResponse($"{animeSongs.Count} random songs found", animeSongs);
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
}
}