rating adding completed
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace WatchIt.Common.Model.Media;
|
||||
|
||||
public class MediaRatingRequest
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
[JsonPropertyName("rating")]
|
||||
public required short Rating { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
[SetsRequiredMembers]
|
||||
public MediaRatingRequest(short rating)
|
||||
{
|
||||
Rating = rating;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace WatchIt.Common.Model.Media;
|
||||
|
||||
public class MediaRatingResponse
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
[JsonPropertyName("rating_average")]
|
||||
public required double RatingAverage { get; set; }
|
||||
|
||||
[JsonPropertyName("rating_count")]
|
||||
public required long RatingCount { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
[JsonConstructor]
|
||||
public MediaRatingResponse() {}
|
||||
|
||||
[SetsRequiredMembers]
|
||||
public MediaRatingResponse(double ratingAverage, long ratingCount)
|
||||
{
|
||||
RatingAverage = ratingAverage;
|
||||
RatingCount = ratingCount;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -12,17 +12,25 @@ namespace WatchIt.WebAPI.Controllers;
|
||||
[Route("media")]
|
||||
public class MediaController(IMediaControllerService mediaControllerService)
|
||||
{
|
||||
#region MAIN
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(MediaResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMedia([FromRoute] long id) => await mediaControllerService.GetMedia(id);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region GENRES
|
||||
|
||||
[HttpGet("{id}/genres")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<GenreResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetGenres([FromRoute]long id) => await mediaControllerService.GetGenres(id);
|
||||
public async Task<ActionResult> GetMediaGenres([FromRoute]long id) => await mediaControllerService.GetMediaGenres(id);
|
||||
|
||||
[HttpPost("{id}/genres/{genre_id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
@@ -30,7 +38,7 @@ public class MediaController(IMediaControllerService mediaControllerService)
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PostGenre([FromRoute]long id, [FromRoute(Name = "genre_id")]short genreId) => await mediaControllerService.PostGenre(id, genreId);
|
||||
public async Task<ActionResult> PostMediaGenre([FromRoute]long id, [FromRoute(Name = "genre_id")]short genreId) => await mediaControllerService.PostMediaGenre(id, genreId);
|
||||
|
||||
[HttpDelete("{id}/genres/{genre_id}")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
@@ -38,20 +46,52 @@ public class MediaController(IMediaControllerService mediaControllerService)
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DeleteGenre([FromRoute]long id, [FromRoute(Name = "genre_id")]short genreId) => await mediaControllerService.DeleteGenre(id, genreId);
|
||||
public async Task<ActionResult> DeleteMediaGenre([FromRoute]long id, [FromRoute(Name = "genre_id")]short genreId) => await mediaControllerService.DeleteMediaGenre(id, genreId);
|
||||
|
||||
[HttpGet("{id}/photos/random_background")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(MediaPhotoResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaRandomBackgroundPhoto([FromRoute]long id) => await mediaControllerService.GetMediaRandomBackgroundPhoto(id);
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region RATING
|
||||
|
||||
[HttpGet("{id}/rating")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(MediaRatingResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaRating([FromRoute] long id) => await mediaControllerService.GetMediaRating(id);
|
||||
|
||||
[HttpGet("{id}/rating/{user_id}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(short), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaRatingByUser([FromRoute] long id, [FromRoute(Name = "user_id")]long userId) => await mediaControllerService.GetMediaRatingByUser(id, userId);
|
||||
|
||||
[HttpPut("{id}/rating")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> PutMediaRating([FromRoute] long id, [FromBody] MediaRatingRequest data) => await mediaControllerService.PutMediaRating(id, data);
|
||||
|
||||
[HttpDelete("{id}/rating")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> DeleteMediaRating([FromRoute] long id) => await mediaControllerService.DeleteMediaRating(id);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region POSTER
|
||||
|
||||
[HttpGet("{id}/poster")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(MediaPosterResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetPoster([FromRoute] long id) => await mediaControllerService.GetPoster(id);
|
||||
public async Task<ActionResult> GetMediaPoster([FromRoute] long id) => await mediaControllerService.GetMediaPoster(id);
|
||||
|
||||
[HttpPut("{id}/poster")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
@@ -59,14 +99,26 @@ public class MediaController(IMediaControllerService mediaControllerService)
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ActionResult> PutPoster([FromRoute]long id, [FromBody]MediaPosterRequest body) => await mediaControllerService.PutPoster(id, body);
|
||||
public async Task<ActionResult> PutMediaPoster([FromRoute]long id, [FromBody]MediaPosterRequest body) => await mediaControllerService.PutMediaPoster(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<ActionResult> DeletePoster([FromRoute]long id) => await mediaControllerService.DeletePoster(id);
|
||||
public async Task<ActionResult> DeleteMediaPoster([FromRoute]long id) => await mediaControllerService.DeleteMediaPoster(id);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PHOTOS
|
||||
|
||||
[HttpGet("{id}/photos/random_background")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(MediaPhotoResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetMediaPhotoRandomBackground([FromRoute]long id) => await mediaControllerService.GetMediaRandomBackgroundPhoto(id);
|
||||
|
||||
[HttpGet("photos/{photo_id}")]
|
||||
[AllowAnonymous]
|
||||
@@ -83,7 +135,7 @@ public class MediaController(IMediaControllerService mediaControllerService)
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(MediaPhotoResponse), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> GetRandomBackgroundPhoto() => await mediaControllerService.GetRandomBackgroundPhoto();
|
||||
public async Task<ActionResult> GetPhotoRandomBackground() => await mediaControllerService.GetRandomBackgroundPhoto();
|
||||
|
||||
[HttpPost("photos")]
|
||||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
@@ -109,4 +161,6 @@ public class MediaController(IMediaControllerService mediaControllerService)
|
||||
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult> DeletePhoto([FromRoute(Name = "photo_id")]Guid photoId) => await mediaControllerService.DeletePhoto(photoId);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -6,16 +6,24 @@ namespace WatchIt.WebAPI.Services.Controllers.Media;
|
||||
public interface IMediaControllerService
|
||||
{
|
||||
Task<RequestResult> GetMedia(long mediaId);
|
||||
Task<RequestResult> GetGenres(long mediaId);
|
||||
Task<RequestResult> PostGenre(long mediaId, short genreId);
|
||||
Task<RequestResult> DeleteGenre(long mediaId, short genreId);
|
||||
|
||||
Task<RequestResult> GetMediaGenres(long mediaId);
|
||||
Task<RequestResult> PostMediaGenre(long mediaId, short genreId);
|
||||
Task<RequestResult> DeleteMediaGenre(long mediaId, short genreId);
|
||||
|
||||
Task<RequestResult> GetMediaRating(long mediaId);
|
||||
Task<RequestResult> GetMediaRatingByUser(long mediaId, long userId);
|
||||
Task<RequestResult> PutMediaRating(long mediaId, MediaRatingRequest data);
|
||||
Task<RequestResult> DeleteMediaRating(long mediaId);
|
||||
|
||||
Task<RequestResult> GetMediaPoster(long id);
|
||||
Task<RequestResult> PutMediaPoster(long id, MediaPosterRequest data);
|
||||
Task<RequestResult> DeleteMediaPoster(long id);
|
||||
|
||||
Task<RequestResult> GetPhoto(Guid id);
|
||||
Task<RequestResult> GetPhotos(MediaPhotoQueryParameters query);
|
||||
Task<RequestResult> GetRandomBackgroundPhoto();
|
||||
Task<RequestResult> GetMediaRandomBackgroundPhoto(long id);
|
||||
Task<RequestResult> GetPoster(long id);
|
||||
Task<RequestResult> PutPoster(long id, MediaPosterRequest data);
|
||||
Task<RequestResult> DeletePoster(long id);
|
||||
Task<RequestResult> PostPhoto(MediaPhotoRequest data);
|
||||
Task<RequestResult> PutPhoto(Guid photoId, MediaPhotoRequest data);
|
||||
Task<RequestResult> DeletePhoto(Guid photoId);
|
||||
|
||||
@@ -4,6 +4,7 @@ using WatchIt.Common.Model.Genres;
|
||||
using WatchIt.Common.Model.Media;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Media;
|
||||
using WatchIt.Database.Model.Rating;
|
||||
using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
|
||||
@@ -13,6 +14,8 @@ public class MediaControllerService(DatabaseContext database, IUserService userS
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task<RequestResult> GetMedia(long mediaId)
|
||||
{
|
||||
Database.Model.Media.Media? item = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
@@ -27,8 +30,12 @@ public class MediaControllerService(DatabaseContext database, IUserService userS
|
||||
|
||||
return RequestResult.Ok(mediaResponse);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetGenres(long mediaId)
|
||||
|
||||
#endregion
|
||||
|
||||
#region Genres
|
||||
|
||||
public async Task<RequestResult> GetMediaGenres(long mediaId)
|
||||
{
|
||||
Database.Model.Media.Media? item = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (item is null)
|
||||
@@ -40,7 +47,7 @@ public class MediaControllerService(DatabaseContext database, IUserService userS
|
||||
return RequestResult.Ok(genres);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PostGenre(long mediaId, short genreId)
|
||||
public async Task<RequestResult> PostMediaGenre(long mediaId, short genreId)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
@@ -65,7 +72,7 @@ public class MediaControllerService(DatabaseContext database, IUserService userS
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteGenre(long mediaId, short genreId)
|
||||
public async Task<RequestResult> DeleteMediaGenre(long mediaId, short genreId)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
if (!validator.IsValid)
|
||||
@@ -86,6 +93,173 @@ public class MediaControllerService(DatabaseContext database, IUserService userS
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
public async Task<RequestResult> GetMediaRating(long mediaId)
|
||||
{
|
||||
Database.Model.Media.Media? item = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
double ratingAverage = item.RatingMedia.Any() ? item.RatingMedia.Average(x => x.Rating) : 0;
|
||||
long ratingCount = item.RatingMedia.Count();
|
||||
MediaRatingResponse ratingResponse = new MediaRatingResponse(ratingAverage, ratingCount);
|
||||
|
||||
return RequestResult.Ok(ratingResponse);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetMediaRatingByUser(long mediaId, long userId)
|
||||
{
|
||||
Database.Model.Media.Media? item = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
short? rating = item.RatingMedia.FirstOrDefault(x => x.AccountId == userId)?.Rating;
|
||||
if (!rating.HasValue)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
return RequestResult.Ok(rating.Value);
|
||||
}
|
||||
|
||||
public async Task<RequestResult> PutMediaRating(long mediaId, MediaRatingRequest data)
|
||||
{
|
||||
short ratingValue = data.Rating;
|
||||
if (ratingValue < 1 || ratingValue > 10)
|
||||
{
|
||||
return RequestResult.BadRequest();
|
||||
}
|
||||
|
||||
Database.Model.Media.Media? item = await database.Media.FirstOrDefaultAsync(x => x.Id == mediaId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
long userId = userService.GetUserId();
|
||||
|
||||
RatingMedia? rating = item.RatingMedia.FirstOrDefault(x => x.AccountId == userId);
|
||||
if (rating is not null)
|
||||
{
|
||||
rating.Rating = ratingValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
rating = new RatingMedia
|
||||
{
|
||||
AccountId = userId,
|
||||
MediaId = mediaId,
|
||||
Rating = ratingValue
|
||||
};
|
||||
await database.RatingsMedia.AddAsync(rating);
|
||||
}
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> DeleteMediaRating(long mediaId)
|
||||
{
|
||||
long userId = userService.GetUserId();
|
||||
|
||||
RatingMedia? item = await database.RatingsMedia.FirstOrDefaultAsync(x => x.MediaId == mediaId && x.AccountId == userId);
|
||||
if (item is null)
|
||||
{
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
database.RatingsMedia.Attach(item);
|
||||
database.RatingsMedia.Remove(item);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Poster
|
||||
|
||||
public async Task<RequestResult> GetMediaPoster(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<RequestResult> PutMediaPoster(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<RequestResult> DeleteMediaPoster(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();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Photos
|
||||
|
||||
public async Task<RequestResult> GetPhoto(Guid id)
|
||||
{
|
||||
MediaPhotoImage? item = await database.MediaPhotoImages.FirstOrDefaultAsync(x => x.Id == id);
|
||||
@@ -129,76 +303,6 @@ public class MediaControllerService(DatabaseContext database, IUserService userS
|
||||
return Task.FromResult<RequestResult>(RequestResult.Ok(data));
|
||||
}
|
||||
|
||||
public async Task<RequestResult> 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<RequestResult> 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<RequestResult> 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<RequestResult> PostPhoto(MediaPhotoRequest data)
|
||||
{
|
||||
UserValidator validator = userService.GetValidator().MustBeAdmin();
|
||||
@@ -284,4 +388,6 @@ public class MediaControllerService(DatabaseContext database, IUserService userS
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -8,4 +8,5 @@ public interface IUserService
|
||||
string? GetRawToken();
|
||||
UserValidator GetValidator();
|
||||
Guid GetJti();
|
||||
long GetUserId();
|
||||
}
|
||||
@@ -40,6 +40,14 @@ public class UserService(DatabaseContext database, IHttpContextAccessor accessor
|
||||
Guid guid = Guid.Parse(jtiClaim.Value);
|
||||
return guid;
|
||||
}
|
||||
|
||||
public long GetUserId()
|
||||
{
|
||||
ClaimsPrincipal user = GetRawUser();
|
||||
Claim subClaim = user.FindFirst(JwtRegisteredClaimNames.Sub)!;
|
||||
long id = long.Parse(subClaim.Value);
|
||||
return id;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using FluentValidation;
|
||||
@@ -5,6 +6,7 @@ using FluentValidation.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.WebAPI.Services.Controllers.Accounts;
|
||||
@@ -26,12 +28,12 @@ public static class Program
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
WebApplication app = WebApplication.CreateBuilder(args)
|
||||
.SetupAuthentication()
|
||||
.SetupDatabase()
|
||||
.SetupWorkerServices()
|
||||
.SetupServices()
|
||||
.SetupApplication()
|
||||
.Build();
|
||||
.SetupAuthentication()
|
||||
.SetupDatabase()
|
||||
.SetupWorkerServices()
|
||||
.SetupServices()
|
||||
.SetupApplication()
|
||||
.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
@@ -40,7 +42,8 @@ public static class Program
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
@@ -56,6 +59,9 @@ public static class Program
|
||||
|
||||
private static WebApplicationBuilder SetupAuthentication(this WebApplicationBuilder builder)
|
||||
{
|
||||
JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
||||
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
||||
|
||||
AuthenticationBuilder authenticationBuilder = builder.Services.AddAuthentication(x =>
|
||||
{
|
||||
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
|
||||
@@ -7,6 +7,10 @@ public class Media
|
||||
public string GetGenres { get; set; }
|
||||
public string PostGenre { get; set; }
|
||||
public string DeleteGenre { get; set; }
|
||||
public string GetMediaRating { get; set; }
|
||||
public string GetMediaRatingByUser { get; set; }
|
||||
public string PutMediaRating { get; set; }
|
||||
public string DeleteMediaRating { get; set; }
|
||||
public string GetPhotoMediaRandomBackground { get; set; }
|
||||
public string GetPoster { get; set; }
|
||||
public string PutPoster { get; set; }
|
||||
|
||||
@@ -5,9 +5,16 @@ namespace WatchIt.Website.Services.WebAPI.Media;
|
||||
|
||||
public interface IMediaWebAPIService
|
||||
{
|
||||
Task Get(long mediaId, Action<MediaResponse> successAction = null, Action? notFoundAction = null);
|
||||
Task GetGenres(long mediaId, Action<IEnumerable<GenreResponse>>? successAction = null, Action? notFoundAction = null);
|
||||
Task PostGenre(long mediaId, long genreId, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null, Action? notFoundAction = null);
|
||||
Task GetMedia(long mediaId, Action<MediaResponse>? successAction = null, Action? notFoundAction = null);
|
||||
|
||||
Task GetMediaGenres(long mediaId, Action<IEnumerable<GenreResponse>>? successAction = null, Action? notFoundAction = null);
|
||||
Task PostMediaGenre(long mediaId, long genreId, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null, Action? notFoundAction = null);
|
||||
|
||||
Task GetMediaRating(long mediaId, Action<MediaRatingResponse>? successAction = null, Action? notFoundAction = null);
|
||||
Task GetMediaRatingByUser(long mediaId, long userId, Action<short>? successAction = null, Action? notFoundAction = null);
|
||||
Task PutMediaRating(long mediaId, MediaRatingRequest body, Action? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null, Action? notFoundAction = null);
|
||||
Task DeleteMediaRating(long mediaId, Action? successAction = null, Action? unauthorizedAction = null);
|
||||
|
||||
Task GetPhotoMediaRandomBackground(long mediaId, Action<MediaPhotoResponse>? successAction = null, Action? notFoundAction = null);
|
||||
Task GetPhotoRandomBackground(Action<MediaPhotoResponse>? successAction = null, Action? notFoundAction = null);
|
||||
Task GetPoster(long mediaId, Action<MediaPosterResponse>? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? notFoundAction = null);
|
||||
|
||||
@@ -10,8 +10,10 @@ namespace WatchIt.Website.Services.WebAPI.Media;
|
||||
public class MediaWebAPIService(IHttpClientService httpClientService, IConfigurationService configurationService) : BaseWebAPIService(configurationService), IMediaWebAPIService
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task Get(long mediaId, Action<MediaResponse> successAction = null, Action? notFoundAction = null)
|
||||
|
||||
#region Main
|
||||
|
||||
public async Task GetMedia(long mediaId, Action<MediaResponse>? successAction = null, Action? notFoundAction = null)
|
||||
{
|
||||
string url = GetUrl(EndpointsConfiguration.Media.Get, mediaId);
|
||||
|
||||
@@ -23,7 +25,11 @@ public class MediaWebAPIService(IHttpClientService httpClientService, IConfigura
|
||||
.ExecuteAction();
|
||||
}
|
||||
|
||||
public async Task GetGenres(long mediaId, Action<IEnumerable<GenreResponse>>? successAction = null, Action? notFoundAction = null)
|
||||
#endregion
|
||||
|
||||
#region Genres
|
||||
|
||||
public async Task GetMediaGenres(long mediaId, Action<IEnumerable<GenreResponse>>? successAction = null, Action? notFoundAction = null)
|
||||
{
|
||||
string url = GetUrl(EndpointsConfiguration.Media.GetGenres, mediaId);
|
||||
|
||||
@@ -35,7 +41,7 @@ public class MediaWebAPIService(IHttpClientService httpClientService, IConfigura
|
||||
.ExecuteAction();
|
||||
}
|
||||
|
||||
public async Task PostGenre(long mediaId, long genreId, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null, Action? notFoundAction = null)
|
||||
public async Task PostMediaGenre(long mediaId, long genreId, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null, Action? notFoundAction = null)
|
||||
{
|
||||
string url = GetUrl(EndpointsConfiguration.Media.PostGenre, mediaId, genreId);
|
||||
|
||||
@@ -49,6 +55,67 @@ public class MediaWebAPIService(IHttpClientService httpClientService, IConfigura
|
||||
.ExecuteAction();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rating
|
||||
|
||||
public async Task GetMediaRating(long mediaId, Action<MediaRatingResponse>? successAction = null, Action? notFoundAction = null)
|
||||
{
|
||||
string url = GetUrl(EndpointsConfiguration.Media.GetMediaRating, mediaId);
|
||||
|
||||
HttpRequest request = new HttpRequest(HttpMethodType.Get, url);
|
||||
|
||||
HttpResponse response = await httpClientService.SendRequestAsync(request);
|
||||
response.RegisterActionFor2XXSuccess(successAction)
|
||||
.RegisterActionFor404NotFound(notFoundAction)
|
||||
.ExecuteAction();
|
||||
}
|
||||
|
||||
public async Task GetMediaRatingByUser(long mediaId, long userId, Action<short>? successAction = null, Action? notFoundAction = null)
|
||||
{
|
||||
string url = GetUrl(EndpointsConfiguration.Media.GetMediaRatingByUser, mediaId, userId);
|
||||
|
||||
HttpRequest request = new HttpRequest(HttpMethodType.Get, url);
|
||||
|
||||
HttpResponse response = await httpClientService.SendRequestAsync(request);
|
||||
response.RegisterActionFor2XXSuccess(successAction)
|
||||
.RegisterActionFor404NotFound(notFoundAction)
|
||||
.ExecuteAction();
|
||||
}
|
||||
|
||||
public async Task PutMediaRating(long mediaId, MediaRatingRequest body, Action? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null, Action? notFoundAction = null)
|
||||
{
|
||||
string url = GetUrl(EndpointsConfiguration.Media.PutMediaRating, mediaId);
|
||||
|
||||
HttpRequest request = new HttpRequest(HttpMethodType.Put, url)
|
||||
{
|
||||
Body = body
|
||||
};
|
||||
|
||||
HttpResponse response = await httpClientService.SendRequestAsync(request);
|
||||
response.RegisterActionFor2XXSuccess(successAction)
|
||||
.RegisterActionFor400BadRequest(badRequestAction)
|
||||
.RegisterActionFor401Unauthorized(unauthorizedAction)
|
||||
.RegisterActionFor404NotFound(notFoundAction)
|
||||
.ExecuteAction();
|
||||
}
|
||||
|
||||
public async Task DeleteMediaRating(long mediaId, Action? successAction = null, Action? unauthorizedAction = null)
|
||||
{
|
||||
string url = GetUrl(EndpointsConfiguration.Media.DeleteMediaRating, mediaId);
|
||||
|
||||
HttpRequest request = new HttpRequest(HttpMethodType.Delete, url);
|
||||
|
||||
HttpResponse response = await httpClientService.SendRequestAsync(request);
|
||||
response.RegisterActionFor2XXSuccess(successAction)
|
||||
.RegisterActionFor401Unauthorized(unauthorizedAction)
|
||||
.ExecuteAction();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
public async Task GetPhotoMediaRandomBackground(long mediaId, Action<MediaPhotoResponse>? successAction = null, Action? notFoundAction = null)
|
||||
{
|
||||
string url = GetUrl(EndpointsConfiguration.Media.GetPhotoMediaRandomBackground, mediaId);
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
<!-- CSS -->
|
||||
<link rel="stylesheet" href="app.css?version=0.15"/>
|
||||
<link rel="stylesheet" href="WatchIt.Website.styles.css?version=0.15"/>
|
||||
<link rel="stylesheet" href="WatchIt.Website.styles.css?version=0.24"/>
|
||||
|
||||
<!-- BOOTSTRAP -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
|
||||
|
||||
@@ -1,10 +1,28 @@
|
||||
@page "/media/{id:long}"
|
||||
@using System.Text
|
||||
@using System.Text
|
||||
@using Microsoft.IdentityModel.Tokens
|
||||
@using WatchIt.Common.Model.Genres
|
||||
|
||||
@page "/media/{id:long}"
|
||||
|
||||
@layout MainLayout
|
||||
|
||||
@if (_loaded)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_error))
|
||||
{
|
||||
<PageTitle>@_media.Title@(_media.ReleaseDate is not null ? $" ({_media.ReleaseDate.Value.Year})" : string.Empty) - WatchIt</PageTitle>
|
||||
}
|
||||
else
|
||||
{
|
||||
<PageTitle>Error - WatchIt</PageTitle>
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
<PageTitle>Loading... - WatchIt</PageTitle>
|
||||
}
|
||||
|
||||
|
||||
|
||||
<div class="container-fluid p-1 gy-2 gx-2">
|
||||
@if (_loaded)
|
||||
@@ -105,8 +123,61 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<div class="rounded-3 panel panel-yellow p-2 h-100">
|
||||
Oceny tutaj będą
|
||||
<div class="rounded-3 panel panel-yellow p-4 h-100">
|
||||
<div class="container-fluid px-0">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<h4 class="text-dark">
|
||||
<strong>Global rating:</strong> @(_globalRating.RatingCount == 0 ? "no ratings" : $"{Math.Round(_globalRating.RatingAverage, 1)}/10")
|
||||
</h4>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<hr class="rating-separator"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<h4 class="text-dark">
|
||||
<strong>Your rating:</strong>
|
||||
</h4>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
@if (_user is not null)
|
||||
{
|
||||
<div class="d-flex rating">
|
||||
<input id="r1" type="radio" name="rate" checked="@(_userRating == 1 )" onclick="@(async () => await AddRating(1 ))"/>
|
||||
<label class="text-dark" for="r1">@(_userRating >= 1 ? "\u2605" : "\u2606")</label>
|
||||
<input id="r2" type="radio" name="rate" checked="@(_userRating == 2 )" onclick="@(async () => await AddRating(2 ))"/>
|
||||
<label class="text-dark" for="r2">@(_userRating >= 2 ? "\u2605" : "\u2606")</label>
|
||||
<input id="r3" type="radio" name="rate" checked="@(_userRating == 3 )" onclick="@(async () => await AddRating(3 ))"/>
|
||||
<label class="text-dark" for="r3">@(_userRating >= 3 ? "\u2605" : "\u2606")</label>
|
||||
<input id="r4" type="radio" name="rate" checked="@(_userRating == 4 )" onclick="@(async () => await AddRating(4 ))"/>
|
||||
<label class="text-dark" for="r4">@(_userRating >= 4 ? "\u2605" : "\u2606")</label>
|
||||
<input id="r5" type="radio" name="rate" checked="@(_userRating == 5 )" onclick="@(async () => await AddRating(5 ))"/>
|
||||
<label class="text-dark" for="r5">@(_userRating >= 5 ? "\u2605" : "\u2606")</label>
|
||||
<input id="r6" type="radio" name="rate" checked="@(_userRating == 6 )" onclick="@(async () => await AddRating(6 ))"/>
|
||||
<label class="text-dark" for="r6">@(_userRating >= 6 ? "\u2605" : "\u2606")</label>
|
||||
<input id="r7" type="radio" name="rate" checked="@(_userRating == 7 )" onclick="@(async () => await AddRating(7 ))"/>
|
||||
<label class="text-dark" for="r7">@(_userRating >= 7 ? "\u2605" : "\u2606")</label>
|
||||
<input id="r8" type="radio" name="rate" checked="@(_userRating == 8 )" onclick="@(async () => await AddRating(8 ))"/>
|
||||
<label class="text-dark" for="r8">@(_userRating >= 8 ? "\u2605" : "\u2606")</label>
|
||||
<input id="r9" type="radio" name="rate" checked="@(_userRating == 9 )" onclick="@(async () => await AddRating(9 ))"/>
|
||||
<label class="text-dark" for="r9">@(_userRating >= 9 ? "\u2605" : "\u2606")</label>
|
||||
<input id="r10" type="radio" name="rate" checked="@(_userRating == 10)" onclick="@(async () => await AddRating(10))"/>
|
||||
<label class="text-dark" for="r10">@(_userRating == 10 ? "\u2605" : "\u2606")</label>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<p class="text-dark">You must be logged in to add a rating</p>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using WatchIt.Common.Model.Genres;
|
||||
using WatchIt.Common.Model.Media;
|
||||
using WatchIt.Common.Model.Movies;
|
||||
@@ -38,9 +39,12 @@ public partial class MediaDataPage : ComponentBase
|
||||
private MediaResponse? _media;
|
||||
private MovieResponse? _movie;
|
||||
private IEnumerable<GenreResponse> _genres;
|
||||
private MediaRatingResponse _globalRating;
|
||||
private MediaPhotoResponse? _background;
|
||||
private MediaPosterResponse? _poster;
|
||||
private User? _user;
|
||||
|
||||
private short? _userRating;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -52,14 +56,15 @@ public partial class MediaDataPage : ComponentBase
|
||||
{
|
||||
if (firstRender)
|
||||
{
|
||||
await MediaWebAPIService.Get(Id, data => _media = data, () => _error = $"Media with id {Id} was not found");
|
||||
await MediaWebAPIService.GetMedia(Id, data => _media = data, () => _error = $"Media with id {Id} was not found");
|
||||
|
||||
if (_error is null)
|
||||
{
|
||||
Task backgroundTask = MediaWebAPIService.GetPhotoMediaRandomBackground(Id, data => _background = data);
|
||||
Task posterTask = MediaWebAPIService.GetPoster(Id, data => _poster = data);
|
||||
Task<User?> userTask = AuthenticationService.GetUserAsync();
|
||||
Task genresTask = MediaWebAPIService.GetGenres(Id, data => _genres = data);
|
||||
Task genresTask = MediaWebAPIService.GetMediaGenres(Id, data => _genres = data);
|
||||
Task globalRatingTask = MediaWebAPIService.GetMediaRating(Id, data => _globalRating = data);
|
||||
Task specificMediaTask;
|
||||
if (_media.Type == MediaType.Movie)
|
||||
{
|
||||
@@ -76,17 +81,43 @@ public partial class MediaDataPage : ComponentBase
|
||||
userTask,
|
||||
specificMediaTask,
|
||||
genresTask,
|
||||
globalRatingTask,
|
||||
backgroundTask,
|
||||
posterTask,
|
||||
]);
|
||||
|
||||
_user = await userTask;
|
||||
}
|
||||
|
||||
if (_user is not null)
|
||||
{
|
||||
Task userRatingTask = MediaWebAPIService.GetMediaRatingByUser(Id, _user.Id, data => _userRating = data);
|
||||
|
||||
await Task.WhenAll(
|
||||
[
|
||||
userRatingTask,
|
||||
]);
|
||||
}
|
||||
|
||||
_loaded = true;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AddRating(short rating)
|
||||
{
|
||||
if (_userRating == rating)
|
||||
{
|
||||
await MediaWebAPIService.DeleteMediaRating(Id);
|
||||
_userRating = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
await MediaWebAPIService.PutMediaRating(Id, new MediaRatingRequest(rating));
|
||||
_userRating = rating;
|
||||
}
|
||||
await MediaWebAPIService.GetMediaRating(Id, data => _globalRating = data);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -15,4 +15,24 @@
|
||||
|
||||
.description-shadow {
|
||||
text-shadow: 1px 1px 1px #000;
|
||||
}
|
||||
|
||||
.rating-separator {
|
||||
border-color: black;
|
||||
border-width: 1px;
|
||||
|
||||
margin: 10px 0;
|
||||
}
|
||||
|
||||
.rating > input {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.rating > label {
|
||||
font-size: 30px;
|
||||
}
|
||||
|
||||
.rating > label:hover, .rating > input:hover+label {
|
||||
color: gray !important;
|
||||
cursor: pointer;
|
||||
}
|
||||
@@ -42,6 +42,11 @@
|
||||
"GetGenres": "/{0}/genres",
|
||||
"PostGenre": "/{0}/genres/{1}",
|
||||
"DeleteGenre": "/{0}/genres/{1}",
|
||||
"GetMediaRating": "/{0}/rating",
|
||||
"GetMediaRatingByUser": "/{0}/rating/{1}",
|
||||
"PutMediaRating": "/{0}/rating",
|
||||
"DeleteMediaRating": "/{0}/rating",
|
||||
|
||||
"GetPhotoMediaRandomBackground": "/{0}/photos/random_background",
|
||||
"GetPoster": "/{0}/poster",
|
||||
"PutPoster": "/{0}/poster",
|
||||
|
||||
Reference in New Issue
Block a user