From cc22e609e1f2af6f126d70cbb1eeb9e9504d6c5c Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Wed, 11 Sep 2024 15:59:13 +0200 Subject: [PATCH] Media poster methods added --- .../Media/MediaPhotoRequest.cs | 1 + .../{MediaPosterImage.cs => MediaPoster.cs} | 2 +- .../Media/MediaPosterRequest.cs | 19 +++++ .../Media/MediaPosterResponse.cs | 36 ++++++++++ .../HttpResponse.cs | 22 +++--- .../Media/MediaPosterImage.cs | 2 +- .../MediaController.cs | 35 ++++++++-- .../IMediaControllerService.cs | 3 + .../MediaControllerService.cs | 70 +++++++++++++++++++ .../Media/MediaPosterRequestValidator.cs | 14 ++++ .../JWTAuthenticationStateProvider.cs | 18 +++-- .../Model/Media.cs | 3 + .../IMediaWebAPIService.cs | 3 + .../MediaWebAPIService.cs | 43 ++++++++++++ .../WatchIt.Website/appsettings.json | 3 + 15 files changed, 250 insertions(+), 24 deletions(-) rename WatchIt.Common/WatchIt.Common.Model/Media/{MediaPosterImage.cs => MediaPoster.cs} (88%) create mode 100644 WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterRequest.cs create mode 100644 WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterResponse.cs create mode 100644 WatchIt.WebAPI/WatchIt.WebAPI.Validators/Media/MediaPosterRequestValidator.cs diff --git a/WatchIt.Common/WatchIt.Common.Model/Media/MediaPhotoRequest.cs b/WatchIt.Common/WatchIt.Common.Model/Media/MediaPhotoRequest.cs index b365b12..956e256 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Media/MediaPhotoRequest.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Media/MediaPhotoRequest.cs @@ -24,6 +24,7 @@ public class MediaPhotoRequest : MediaPhoto item.MediaId = MediaId; item.Image = Image; item.MimeType = MimeType; + item.UploadDate = DateTime.Now; } public void UpdateMediaPhotoImageBackground(MediaPhotoImageBackground item) diff --git a/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterImage.cs b/WatchIt.Common/WatchIt.Common.Model/Media/MediaPoster.cs similarity index 88% rename from WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterImage.cs rename to WatchIt.Common/WatchIt.Common.Model/Media/MediaPoster.cs index 4044a30..dfa79e8 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterImage.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Media/MediaPoster.cs @@ -2,7 +2,7 @@ namespace WatchIt.Common.Model.Media; -public class MediaPosterImage +public abstract class MediaPoster { [JsonPropertyName("image")] public required byte[] Image { get; set; } diff --git a/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterRequest.cs b/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterRequest.cs new file mode 100644 index 0000000..7e8224e --- /dev/null +++ b/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterRequest.cs @@ -0,0 +1,19 @@ +using WatchIt.Database.Model.Media; + +namespace WatchIt.Common.Model.Media; + +public class MediaPosterRequest : MediaPoster +{ + public MediaPosterImage CreateMediaPosterImage() => new MediaPosterImage + { + Image = Image, + MimeType = MimeType, + }; + + public void UpdateMediaPosterImage(MediaPosterImage item) + { + item.Image = Image; + item.MimeType = MimeType; + item.UploadDate = DateTime.Now; + } +} \ No newline at end of file diff --git a/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterResponse.cs b/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterResponse.cs new file mode 100644 index 0000000..f2c1ff4 --- /dev/null +++ b/WatchIt.Common/WatchIt.Common.Model/Media/MediaPosterResponse.cs @@ -0,0 +1,36 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; +using WatchIt.Database.Model.Media; + +namespace WatchIt.Common.Model.Media; + +public class MediaPosterResponse : MediaPoster +{ + #region PROPERTIES + + [JsonPropertyName("id")] + public Guid Id { get; set; } + + [JsonPropertyName("upload_date")] + public DateTime UploadDate { get; set; } + + #endregion + + + + #region CONSTRUCTORS + + [JsonConstructor] + public MediaPosterResponse() {} + + [SetsRequiredMembers] + public MediaPosterResponse(MediaPosterImage mediaPhotoImage) + { + Id = mediaPhotoImage.Id; + Image = mediaPhotoImage.Image; + MimeType = mediaPhotoImage.MimeType; + UploadDate = mediaPhotoImage.UploadDate; + } + + #endregion +} \ No newline at end of file diff --git a/WatchIt.Common/WatchIt.Common.Services/WatchIt.Common.Services.HttpClient/HttpResponse.cs b/WatchIt.Common/WatchIt.Common.Services/WatchIt.Common.Services.HttpClient/HttpResponse.cs index 0edbd6b..4180f6e 100644 --- a/WatchIt.Common/WatchIt.Common.Services/WatchIt.Common.Services.HttpClient/HttpResponse.cs +++ b/WatchIt.Common/WatchIt.Common.Services/WatchIt.Common.Services.HttpClient/HttpResponse.cs @@ -7,7 +7,7 @@ public class HttpResponse { #region FIELDS - private HttpResponseMessage _message; + private readonly HttpResponseMessage _message; private Action? _2XXAction; private Action? _400Action; @@ -32,37 +32,37 @@ public class HttpResponse #region PUBLIC METHODS - public HttpResponse RegisterActionFor2XXSuccess(Action action) + public HttpResponse RegisterActionFor2XXSuccess(Action? action) { _2XXAction = action; return this; } - public HttpResponse RegisterActionFor2XXSuccess(Action action) + public HttpResponse RegisterActionFor2XXSuccess(Action? action) { _2XXAction = () => Invoke(action); return this; } - public HttpResponse RegisterActionFor400BadRequest(Action> action) + public HttpResponse RegisterActionFor400BadRequest(Action>? action) { _400Action = () => Invoke(action); return this; } - public HttpResponse RegisterActionFor401Unauthorized(Action action) + public HttpResponse RegisterActionFor401Unauthorized(Action? action) { _401Action = action; return this; } - public HttpResponse RegisterActionFor403Forbidden(Action action) + public HttpResponse RegisterActionFor403Forbidden(Action? action) { _403Action = action; return this; } - public HttpResponse RegisterActionFor404NotFound(Action action) + public HttpResponse RegisterActionFor404NotFound(Action? action) { _404Action = action; return this; @@ -86,21 +86,21 @@ public class HttpResponse #region PRIVATE METHODS - private async void Invoke(Action action) + private async void Invoke(Action? action) { Stream streamData = await _message.Content.ReadAsStreamAsync(); T? data = await JsonSerializer.DeserializeAsync(streamData); - action.Invoke(data!); + action?.Invoke(data!); } - private async void Invoke(Action> action) + private async void Invoke(Action>? action) { Stream streamData = await _message.Content.ReadAsStreamAsync(); ValidationProblemDetails? data = await JsonSerializer.DeserializeAsync(streamData, new JsonSerializerOptions { PropertyNameCaseInsensitive = true, }); - action.Invoke(data!.Errors); + action?.Invoke(data!.Errors); } #endregion diff --git a/WatchIt.Database/WatchIt.Database.Model/WatchIt.Database.Model/Media/MediaPosterImage.cs b/WatchIt.Database/WatchIt.Database.Model/WatchIt.Database.Model/Media/MediaPosterImage.cs index c52c908..046e838 100644 --- a/WatchIt.Database/WatchIt.Database.Model/WatchIt.Database.Model/Media/MediaPosterImage.cs +++ b/WatchIt.Database/WatchIt.Database.Model/WatchIt.Database.Model/Media/MediaPosterImage.cs @@ -7,7 +7,7 @@ public class MediaPosterImage public Guid Id { get; set; } public required byte[] Image { get; set; } public required string MimeType { get; set; } - public required DateTime UploadDate { get; set; } + public DateTime UploadDate { get; set; } #endregion diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/MediaController.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/MediaController.cs index 09b41d5..de9adc1 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/MediaController.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/MediaController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using WatchIt.Common.Model.Genres; @@ -18,7 +19,7 @@ public class MediaController(IMediaControllerService mediaControllerService) public async Task GetGenres([FromRoute]long id) => await mediaControllerService.GetGenres(id); [HttpPost("{id}/genres/{genre_id}")] - [Authorize] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] @@ -26,7 +27,7 @@ public class MediaController(IMediaControllerService mediaControllerService) public async Task PostGenre([FromRoute]long id, [FromRoute(Name = "genre_id")]short genreId) => await mediaControllerService.PostGenre(id, genreId); [HttpDelete("{id}/genres/{genre_id}")] - [Authorize] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] @@ -38,6 +39,28 @@ public class MediaController(IMediaControllerService mediaControllerService) [ProducesResponseType(typeof(MediaPhotoResponse), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetMediaRandomBackgroundPhoto([FromRoute]long id) => await mediaControllerService.GetMediaRandomBackgroundPhoto(id); + + [HttpGet("{id}/poster")] + [AllowAnonymous] + [ProducesResponseType(typeof(MediaPosterResponse), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task GetPoster([FromRoute] long id) => await mediaControllerService.GetPoster(id); + + [HttpPut("{id}/poster")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] + public async Task PutPoster([FromRoute]long id, [FromBody]MediaPosterRequest body) => await mediaControllerService.PutPoster(id, body); + + [HttpDelete("{id}/poster")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + [ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)] + [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] + public async Task DeletePoster([FromRoute]long id) => await mediaControllerService.DeletePoster(id); [HttpGet("photos/{photo_id}")] [AllowAnonymous] @@ -57,7 +80,7 @@ public class MediaController(IMediaControllerService mediaControllerService) public async Task GetRandomBackgroundPhoto() => await mediaControllerService.GetRandomBackgroundPhoto(); [HttpPost("photos")] - [Authorize] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [ProducesResponseType(typeof(MediaPhotoResponse), StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] @@ -65,7 +88,7 @@ public class MediaController(IMediaControllerService mediaControllerService) public async Task PostPhoto([FromBody]MediaPhotoRequest body) => await mediaControllerService.PostPhoto(body); [HttpPut("photos/{photo_id}")] - [Authorize] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] @@ -74,7 +97,7 @@ public class MediaController(IMediaControllerService mediaControllerService) public async Task PutPhoto([FromRoute(Name = "photo_id")]Guid photoId, [FromBody]MediaPhotoRequest body) => await mediaControllerService.PutPhoto(photoId, body); [HttpDelete("photos/{photo_id}")] - [Authorize] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Media/IMediaControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Media/IMediaControllerService.cs index dd87114..0e63173 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Media/IMediaControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Media/IMediaControllerService.cs @@ -12,6 +12,9 @@ public interface IMediaControllerService Task GetPhotos(MediaPhotoQueryParameters query); Task GetRandomBackgroundPhoto(); Task GetMediaRandomBackgroundPhoto(long id); + Task GetPoster(long id); + Task PutPoster(long id, MediaPosterRequest data); + Task DeletePoster(long id); Task PostPhoto(MediaPhotoRequest data); Task PutPhoto(Guid photoId, MediaPhotoRequest data); Task DeletePhoto(Guid photoId); diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Media/MediaControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Media/MediaControllerService.cs index 15d5382..b67ad58 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Media/MediaControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Media/MediaControllerService.cs @@ -114,6 +114,76 @@ public class MediaControllerService(DatabaseContext database, IUserService userS return Task.FromResult(RequestResult.Ok(data)); } + public async Task GetPoster(long id) + { + Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == id); + if (media is null) + { + return RequestResult.BadRequest(); + } + + MediaPosterImage? poster = media.MediaPosterImage; + if (poster is null) + { + return RequestResult.NotFound(); + } + + MediaPosterResponse data = new MediaPosterResponse(poster); + return RequestResult.Ok(data); + } + + public async Task PutPoster(long id, MediaPosterRequest data) + { + UserValidator validator = userService.GetValidator().MustBeAdmin(); + if (!validator.IsValid) + { + return RequestResult.Forbidden(); + } + + Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == id); + if (media is null) + { + return RequestResult.BadRequest(); + } + + if (media.MediaPosterImage is null) + { + MediaPosterImage image = data.CreateMediaPosterImage(); + await database.MediaPosterImages.AddAsync(image); + await database.SaveChangesAsync(); + + media.MediaPosterImageId = image.Id; + } + else + { + data.UpdateMediaPosterImage(media.MediaPosterImage); + } + + await database.SaveChangesAsync(); + + return RequestResult.Ok(); + } + + public async Task DeletePoster(long id) + { + UserValidator validator = userService.GetValidator().MustBeAdmin(); + if (!validator.IsValid) + { + return RequestResult.Forbidden(); + } + + Database.Model.Media.Media? media = await database.Media.FirstOrDefaultAsync(x => x.Id == id); + + if (media?.MediaPosterImage != null) + { + database.MediaPosterImages.Attach(media.MediaPosterImage); + database.MediaPosterImages.Remove(media.MediaPosterImage); + await database.SaveChangesAsync(); + } + + return RequestResult.NoContent(); + } + public async Task PostPhoto(MediaPhotoRequest data) { UserValidator validator = userService.GetValidator().MustBeAdmin(); diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Validators/Media/MediaPosterRequestValidator.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Validators/Media/MediaPosterRequestValidator.cs new file mode 100644 index 0000000..1ba3d2f --- /dev/null +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Validators/Media/MediaPosterRequestValidator.cs @@ -0,0 +1,14 @@ +using FluentValidation; +using WatchIt.Common.Model.Media; +using WatchIt.Database; + +namespace WatchIt.WebAPI.Validators.Media; + +public class MediaPosterRequestValidator : AbstractValidator +{ + public MediaPosterRequestValidator() + { + RuleFor(x => x.Image).NotEmpty(); + RuleFor(x => x.MimeType).Matches(@"\w+/.+").WithMessage("Incorrect mimetype"); + } +} \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Authentication/JWTAuthenticationStateProvider.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Authentication/JWTAuthenticationStateProvider.cs index b5f9490..8e46e30 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Authentication/JWTAuthenticationStateProvider.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Authentication/JWTAuthenticationStateProvider.cs @@ -4,9 +4,10 @@ using System.Text.Json; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.Extensions.Logging; using WatchIt.Common.Model.Accounts; +using WatchIt.Website.Services.Utility.Tokens; using WatchIt.Website.Services.WebAPI.Accounts; -namespace WatchIt.Website.Services.Utility.Tokens; +namespace WatchIt.Website.Services.Utility.Authentication; public class JWTAuthenticationStateProvider : AuthenticationStateProvider { @@ -111,13 +112,20 @@ public class JWTAuthenticationStateProvider : AuthenticationStateProvider private async Task Refresh(string refreshToken) { - AuthenticateResponse response = null; + AuthenticateResponse? response = null; await _accountsService.AuthenticateRefresh((data) => response = data); + + if (response is not null) + { + await _tokensService.SaveAuthenticationData(response); + } + else + { + await _tokensService.RemoveAuthenticationData(); + } - await _tokensService.SaveAuthenticationData(response); - - return response.AccessToken; + return response?.AccessToken; } private static IEnumerable GetClaimsFromToken(string token) diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Media.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Media.cs index 5a576a2..f7adb93 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Media.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.Utility/WatchIt.Website.Services.Utility.Configuration/Model/Media.cs @@ -7,6 +7,9 @@ public class Media public string PostGenre { get; set; } public string DeleteGenre { get; set; } public string GetPhotoMediaRandomBackground { get; set; } + public string GetPoster { get; set; } + public string PutPoster { get; set; } + public string DeletePoster { get; set; } public string GetPhoto { get; set; } public string GetPhotos { get; set; } public string GetPhotoRandomBackground { get; set; } diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Media/IMediaWebAPIService.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Media/IMediaWebAPIService.cs index 4a58fbf..9c79dec 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Media/IMediaWebAPIService.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Media/IMediaWebAPIService.cs @@ -8,4 +8,7 @@ public interface IMediaWebAPIService Task GetGenres(long mediaId, Action>? successAction = null, Action? notFoundAction = null); Task PostGenre(long mediaId, long genreId, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null, Action? notFoundAction = null); Task GetPhotoRandomBackground(Action? successAction = null, Action? notFoundAction = null); + Task GetPoster(long mediaId, Action? successAction = null, Action>? badRequestAction = null, Action? notFoundAction = null); + Task PutPoster(long mediaId, MediaPosterRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null); + Task DeletePoster(long mediaId, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null); } \ No newline at end of file diff --git a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Media/MediaWebAPIService.cs b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Media/MediaWebAPIService.cs index 34f8fe6..79de26a 100644 --- a/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Media/MediaWebAPIService.cs +++ b/WatchIt.Website/WatchIt.Website.Services/WatchIt.Website.Services.WebAPI/WatchIt.Website.Services.WebAPI.Media/MediaWebAPIService.cs @@ -49,6 +49,49 @@ public class MediaWebAPIService(IHttpClientService httpClientService, IConfigura .ExecuteAction(); } + public async Task GetPoster(long mediaId, Action? successAction = null, Action>? badRequestAction = null, Action? notFoundAction = null) + { + string url = GetUrl(EndpointsConfiguration.Media.GetPoster, mediaId); + + HttpRequest request = new HttpRequest(HttpMethodType.Get, url); + + HttpResponse response = await httpClientService.SendRequestAsync(request); + response.RegisterActionFor2XXSuccess(successAction) + .RegisterActionFor400BadRequest(badRequestAction) + .RegisterActionFor404NotFound(notFoundAction) + .ExecuteAction(); + } + + public async Task PutPoster(long mediaId, MediaPosterRequest data, Action? successAction = null, Action>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null) + { + string url = GetUrl(EndpointsConfiguration.Media.PutPoster, mediaId); + + HttpRequest request = new HttpRequest(HttpMethodType.Get, url) + { + Body = data + }; + + HttpResponse response = await httpClientService.SendRequestAsync(request); + response.RegisterActionFor2XXSuccess(successAction) + .RegisterActionFor400BadRequest(badRequestAction) + .RegisterActionFor401Unauthorized(unauthorizedAction) + .RegisterActionFor403Forbidden(forbiddenAction) + .ExecuteAction(); + } + + public async Task DeletePoster(long mediaId, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null) + { + string url = GetUrl(EndpointsConfiguration.Media.DeletePoster, mediaId); + + HttpRequest request = new HttpRequest(HttpMethodType.Delete, url); + + HttpResponse response = await httpClientService.SendRequestAsync(request); + response.RegisterActionFor2XXSuccess(successAction) + .RegisterActionFor401Unauthorized(unauthorizedAction) + .RegisterActionFor403Forbidden(forbiddenAction) + .ExecuteAction(); + } + #endregion diff --git a/WatchIt.Website/WatchIt.Website/appsettings.json b/WatchIt.Website/WatchIt.Website/appsettings.json index a7f47be..76032c8 100644 --- a/WatchIt.Website/WatchIt.Website/appsettings.json +++ b/WatchIt.Website/WatchIt.Website/appsettings.json @@ -42,6 +42,9 @@ "PostGenre": "/{0}/genres/{1}", "DeleteGenre": "/{0}/genres/{1}", "GetPhotoMediaRandomBackground": "/{0}/photos/random_background", + "GetPoster": "/{0}/poster", + "PutPoster": "/{0}/poster", + "DeletePoster": "/{0}/poster", "GetPhoto": "/photos/{0}", "GetPhotos": "/photos", "GetPhotoRandomBackground": "/photos/random_background",