|
| 1 | +using System.Net; |
| 2 | +using System.Net.Mime; |
| 3 | +using System.Text.Json; |
| 4 | +using Microsoft.AspNetCore.Http.Extensions; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using Microsoft.Extensions.Options; |
| 7 | +using Swashbuckle.AspNetCore.Annotations; |
| 8 | +using TNO.API.Areas.Admin.Models.MediaAnalytics; |
| 9 | +using TNO.API.Models; |
| 10 | +using TNO.Core.Exceptions; |
| 11 | +using TNO.DAL.Services; |
| 12 | +using TNO.Entities; |
| 13 | +using TNO.Entities.Models; |
| 14 | +using TNO.Keycloak; |
| 15 | + |
| 16 | +namespace TNO.API.Areas.Admin.Controllers; |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// MediaTypeController class, provides MediaType endpoints for the api. |
| 20 | +/// </summary> |
| 21 | +///[ClientRoleAuthorize(ClientRole.Administrator)] |
| 22 | +[ApiController] |
| 23 | +[Area("admin")] |
| 24 | +[ApiVersion("1.0")] |
| 25 | +[Route("api/v{version:apiVersion}/[area]/media-analytics")] |
| 26 | +[Route("api/[area]/media-analytics")] |
| 27 | +[Route("v{version:apiVersion}/[area]/media-analytics")] |
| 28 | +[Route("[area]/media-analytics")] |
| 29 | +[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.Unauthorized)] |
| 30 | +[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.Forbidden)] |
| 31 | +public class MediaAnalyticsController : ControllerBase |
| 32 | +{ |
| 33 | + #region Variables |
| 34 | + private readonly IMediaAnalyticsService _service; |
| 35 | + private readonly JsonSerializerOptions _serializerOptions; |
| 36 | + #endregion |
| 37 | + |
| 38 | + #region Constructors |
| 39 | + /// <summary> |
| 40 | + /// Creates a new instance of a MediaTypeController object, initializes with specified parameters. |
| 41 | + /// </summary> |
| 42 | + /// <param name="service"></param> |
| 43 | + /// <param name="serializerOptions"></param> |
| 44 | + public MediaAnalyticsController(IMediaAnalyticsService service, IOptions<JsonSerializerOptions> serializerOptions) |
| 45 | + { |
| 46 | + _service = service; |
| 47 | + _serializerOptions = serializerOptions.Value; |
| 48 | + } |
| 49 | + #endregion |
| 50 | + |
| 51 | + #region Endpoints |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Find a page of user for the specified query filter. |
| 55 | + /// </summary> |
| 56 | + /// <returns></returns> |
| 57 | + [HttpGet] |
| 58 | + [Produces(MediaTypeNames.Application.Json)] |
| 59 | + [ProducesResponseType(typeof(IPaged<MediaAnalyticsModel>), (int)HttpStatusCode.OK)] |
| 60 | + [SwaggerOperation(Tags = new[] { "User" })] |
| 61 | + public IActionResult Find() |
| 62 | + { |
| 63 | + var uri = new Uri(this.Request.GetDisplayUrl()); |
| 64 | + var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query); |
| 65 | + var result = _service.Find(new TNO.Models.Filters.MediaAnalyticsFilter(query)); |
| 66 | + var page = new Paged<MediaAnalyticsModel>(result.Items.Select(u => new MediaAnalyticsModel(u, _serializerOptions)), result.Page, result.Quantity, result.Total); |
| 67 | + return new JsonResult(page); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Find content for the specified 'id'. |
| 72 | + /// </summary> |
| 73 | + /// <param name="id"></param> |
| 74 | + /// <returns></returns> |
| 75 | + [HttpGet("{id}")] |
| 76 | + [Produces(MediaTypeNames.Application.Json)] |
| 77 | + [ProducesResponseType(typeof(MediaAnalyticsModel), (int)HttpStatusCode.OK)] |
| 78 | + [ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)] |
| 79 | + [SwaggerOperation(Tags = new[] { "MediaType" })] |
| 80 | + public IActionResult FindById(int id) |
| 81 | + { |
| 82 | + var result = _service.FindById(id) ?? throw new NoContentException(); |
| 83 | + return new JsonResult(new MediaAnalyticsModel(result, _serializerOptions)); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Add content for the specified 'id'. |
| 88 | + /// </summary> |
| 89 | + /// <param name="model"></param> |
| 90 | + /// <returns></returns> |
| 91 | + [HttpPost] |
| 92 | + [Produces(MediaTypeNames.Application.Json)] |
| 93 | + [ProducesResponseType(typeof(MediaAnalyticsModel), (int)HttpStatusCode.Created)] |
| 94 | + [ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)] |
| 95 | + [SwaggerOperation(Tags = new[] { "MediaType" })] |
| 96 | + public IActionResult Add([FromBody] MediaAnalyticsModel model) |
| 97 | + { |
| 98 | + var result = _service.AddAndSave((Entities.MediaAnalytics)model); |
| 99 | + return CreatedAtAction(nameof(FindById), new { id = result.Id }, new MediaAnalyticsModel(result, _serializerOptions)); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Update content for the specified 'id'. |
| 104 | + /// </summary> |
| 105 | + /// <param name="model"></param> |
| 106 | + /// <returns></returns> |
| 107 | + [HttpPut("{id}")] |
| 108 | + [Produces(MediaTypeNames.Application.Json)] |
| 109 | + [ProducesResponseType(typeof(MediaAnalyticsModel), (int)HttpStatusCode.OK)] |
| 110 | + [ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)] |
| 111 | + [SwaggerOperation(Tags = new[] { "MediaType" })] |
| 112 | + public IActionResult Update([FromBody] MediaAnalyticsModel model) |
| 113 | + { |
| 114 | + var result = _service.UpdateAndSave((Entities.MediaAnalytics)model); |
| 115 | + return new JsonResult(new MediaAnalyticsModel(result, _serializerOptions)); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Delete content for the specified 'id'. |
| 120 | + /// </summary> |
| 121 | + /// <param name="model"></param> |
| 122 | + /// <returns></returns> |
| 123 | + [HttpDelete("{id}")] |
| 124 | + [Produces(MediaTypeNames.Application.Json)] |
| 125 | + [ProducesResponseType(typeof(MediaAnalyticsModel), (int)HttpStatusCode.OK)] |
| 126 | + [ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)] |
| 127 | + [SwaggerOperation(Tags = new[] { "MediaType" })] |
| 128 | + public IActionResult Delete([FromBody] MediaAnalyticsModel model) |
| 129 | + { |
| 130 | + _service.DeleteAndSave((Entities.MediaAnalytics)model); |
| 131 | + return new JsonResult(model); |
| 132 | + } |
| 133 | + #endregion |
| 134 | +} |
0 commit comments