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 pathEpisodeController.cs
More file actions
106 lines (97 loc) · 3.28 KB
/
Copy pathEpisodeController.cs
File metadata and controls
106 lines (97 loc) · 3.28 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
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Commons;
using Commons.Collections;
using Commons.Enums;
using Commons.Filters;
using System;
using System.Collections.Generic;
using System.Net;
using WebAPI.Models;
using MongoService;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Authorization;
namespace WebAPI.Controllers
{
/// <summary>
/// R Controller for Episode resource
/// </summary>
[ApiVersion("1")]
[Route("episode")]
[ApiController]
public class EpisodeController : Controller
{
private readonly ILogger<EpisodeController> _logger;
private EpisodeCollection _episodeCollection = new EpisodeCollection();
public EpisodeController(ILogger<EpisodeController> logger)
{
_logger = logger;
}
/// <summary>
/// Retrieves a specific Episode by id
/// </summary>
/// <param name="id">The Episode id</param>
[AllowAnonymous]
[EnableCors("CorsEveryone")]
[HttpGet("{id}"), MapToApiVersion("1")]
public APIResponse GetOne(long id)
{
try
{
Episode episode = this._episodeCollection.Get(id);
if (episode == null)
{
throw new APIException(HttpStatusCode.NotFound,
"Episode not found",
$"Episode with id {id} does not exists");
}
return APIManager.SuccessResponse("Episode found", episode);
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch(Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
/// <summary>
/// Retrieves a list of Episode
/// </summary>
/// <param name="filter">The Episode filter</param>
[AllowAnonymous]
[EnableCors("CorsEveryone")]
[HttpGet, MapToApiVersion("1")]
public APIResponse GetMore([FromQuery] EpisodeFilter filter)
{
try
{
Paging<Episode> result = this._episodeCollection.GetList<EpisodeFilter>(filter);
if(result.LastPage == 0)
{
throw new APIException(HttpStatusCode.NotFound,
"Zero episode found",
new List<Episode>());
}
if(filter.page > result.LastPage)
{
throw new APIException(HttpStatusCode.NotFound,
"Page out of range",
$"Last page number available is {result.LastPage}");
}
return APIManager.SuccessResponse($"Page {result.CurrentPage} contains {result.Documents.Count} episodes. Last page number is {result.LastPage} for a total of {result.Count} episodes", result);
}
catch(APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch(Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
}
}