From 28dcc102e67759d6edefdc2cb08c6e6baa62c54c Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Sat, 19 Oct 2024 17:29:36 +0200 Subject: [PATCH] person view add endpoint added --- .../PersonsController.cs | 8 ++++- .../IPersonsControllerService.cs | 1 + .../PersonsControllerService.cs | 30 +++++++++++++++++++ .../Model/Persons.cs | 1 + .../PersonsWebAPIService.cs | 12 ++++++++ .../WatchIt.Website/appsettings.json | 1 + 6 files changed, 52 insertions(+), 1 deletion(-) diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/PersonsController.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/PersonsController.cs index 0192a0d..a36a45e 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/PersonsController.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/PersonsController.cs @@ -77,7 +77,13 @@ public class PersonsController : ControllerBase [AllowAnonymous] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] - public async Task GetMoviesViewRank([FromQuery] int first = 5, [FromQuery] int days = 7) => await _personsControllerService.GetPersonsViewRank(first, days); + public async Task GetPersonsViewRank([FromQuery] int first = 5, [FromQuery] int days = 7) => await _personsControllerService.GetPersonsViewRank(first, days); + + [HttpPost("{id}/view")] + [AllowAnonymous] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task PostPersonsView([FromRoute] long id) => await _personsControllerService.PostPersonsView(id); #endregion diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/IPersonsControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/IPersonsControllerService.cs index 8b72b75..b5802ce 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/IPersonsControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/IPersonsControllerService.cs @@ -13,6 +13,7 @@ public interface IPersonsControllerService Task DeletePerson(long id); Task GetPersonsViewRank(int first, int days); + Task PostPersonsView(long personId); Task GetPersonPhoto(long id); Task PutPersonPhoto(long id, PersonPhotoRequest data); diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/PersonsControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/PersonsControllerService.cs index 2118ce2..daafedf 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/PersonsControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Persons/PersonsControllerService.cs @@ -3,6 +3,7 @@ using WatchIt.Common.Model.Persons; using WatchIt.Common.Model.Roles; using WatchIt.Database; using WatchIt.Database.Model.Person; +using WatchIt.Database.Model.ViewCount; using WatchIt.WebAPI.Services.Controllers.Common; using WatchIt.WebAPI.Services.Utility.User; using Person = WatchIt.Database.Model.Person.Person; @@ -141,6 +142,35 @@ public class PersonsControllerService : IPersonsControllerService return RequestResult.Ok(data); } + + public async Task PostPersonsView(long personId) + { + Database.Model.Media.Media? item = await _database.Media.FirstOrDefaultAsync(x => x.Id == personId); + if (item is null) + { + return RequestResult.NotFound(); + } + + DateOnly dateNow = DateOnly.FromDateTime(DateTime.Now); + ViewCountPerson? viewCount = await _database.ViewCountsPerson.FirstOrDefaultAsync(x => x.PersonId == personId && x.Date == dateNow); + if (viewCount is null) + { + viewCount = new ViewCountPerson + { + PersonId = personId, + Date = dateNow, + ViewCount = 1 + }; + await _database.ViewCountsPerson.AddAsync(viewCount); + } + else + { + viewCount.ViewCount++; + } + await _database.SaveChangesAsync(); + + return RequestResult.Ok(); + } #endregion diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Persons.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Persons.cs index 4a1381f..4f372c7 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Persons.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Persons.cs @@ -9,6 +9,7 @@ public class Persons public string PutPerson { get; set; } public string DeletePerson { get; set; } public string GetPersonsViewRank { get; set; } + public string PostPersonsView { get; set; } public string GetPersonPhoto { get; set; } public string PutPersonPhoto { get; set; } public string DeletePersonPhoto { get; set; } diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/PersonsWebAPIService.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/PersonsWebAPIService.cs index b7d6ed4..cd1fd70 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/PersonsWebAPIService.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Persons/PersonsWebAPIService.cs @@ -138,6 +138,18 @@ public class PersonsWebAPIService : BaseWebAPIService, IPersonsWebAPIService .RegisterActionFor400BadRequest(badRequestAction) .ExecuteAction(); } + + public async Task PostPersonsView(long personId, Action? successAction = null, Action? notFoundAction = null) + { + string url = GetUrl(EndpointsConfiguration.Persons.PostPersonsView, personId); + + HttpRequest request = new HttpRequest(HttpMethodType.Post, url); + + HttpResponse response = await _httpClientService.SendRequestAsync(request); + response.RegisterActionFor2XXSuccess(successAction) + .RegisterActionFor404NotFound(notFoundAction) + .ExecuteAction(); + } #endregion diff --git a/WatchIt.Website/WatchIt.Website/appsettings.json b/WatchIt.Website/WatchIt.Website/appsettings.json index e9ee01b..4417577 100644 --- a/WatchIt.Website/WatchIt.Website/appsettings.json +++ b/WatchIt.Website/WatchIt.Website/appsettings.json @@ -94,6 +94,7 @@ "PutPerson": "/{0}", "DeletePerson": "/{0}", "GetPersonsViewRank": "/view", + "PostPersonsView": "/{0}/view", "GetPersonPhoto": "/{0}/photo", "PutPersonPhoto": "/{0}/photo", "DeletePersonPhoto": "/{0}/photo",