This commit is contained in:
2024-07-03 22:18:32 +02:00
Unverified
parent 4b333878b8
commit 008dcdf4ec
88 changed files with 3389 additions and 644 deletions

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using WatchIt.Common.Query;
namespace WatchIt.Common.Model.Genres;

View File

@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;
namespace WatchIt.Common.Model.Media;
public class MediaPhoto
{
[JsonPropertyName("media_id")]
public required long MediaId { get; set; }
[JsonPropertyName("image")]
public required byte[] Image { get; set; }
[JsonPropertyName("mime_type")]
public required string MimeType { get; set; }
[JsonPropertyName("background")]
public MediaPhotoBackground? Background { get; set; }
}

View File

@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;
namespace WatchIt.Common.Model.Media;
public class MediaPhotoBackground
{
[JsonPropertyName("is_universal_background")]
public required bool IsUniversalBackground { get; set; }
[JsonPropertyName("first_gradient_color")]
public required byte[] FirstGradientColor { get; set; }
[JsonPropertyName("second_gradient_color")]
public required byte[] SecondGradientColor { get; set; }
}

View File

@@ -0,0 +1,36 @@
using System.Text;
using Microsoft.AspNetCore.Mvc;
using WatchIt.Common.Query;
namespace WatchIt.Common.Model.Media;
public class MediaPhotoQueryParameters : QueryParameters<MediaPhotoResponse>
{
#region PROPERTIES
[FromQuery(Name = "mime_type")]
public string? MimeType { get; set; }
[FromQuery(Name = "is_background")]
public bool? IsBackground { get; set; }
[FromQuery(Name = "is_universal_background")]
public bool? IsUniversalBackground { get; set; }
#endregion
#region PUBLIC METHODS
public override bool IsMeetingConditions(MediaPhotoResponse item) =>
(
TestString(item.MimeType, MimeType)
&&
TestBoolean(item.Background is not null, IsBackground)
&&
TestBoolean(item.Background!.IsUniversalBackground, IsUniversalBackground)
);
#endregion
}

View File

@@ -0,0 +1,38 @@
using WatchIt.Database.Model.Media;
namespace WatchIt.Common.Model.Media;
public class MediaPhotoRequest : MediaPhoto
{
public MediaPhotoImage CreateMediaPhotoImage() => new MediaPhotoImage
{
MediaId = MediaId,
Image = Image,
MimeType = MimeType
};
public MediaPhotoImageBackground? CreateMediaPhotoImageBackground(Guid mediaPhotoImageId) => Background is null ? null : new MediaPhotoImageBackground
{
Id = mediaPhotoImageId,
IsUniversalBackground = Background.IsUniversalBackground,
FirstGradientColor = Background.FirstGradientColor,
SecondGradientColor = Background.SecondGradientColor
};
public void UpdateMediaPhotoImage(MediaPhotoImage item)
{
item.MediaId = MediaId;
item.Image = Image;
item.MimeType = MimeType;
}
public void UpdateMediaPhotoImageBackground(MediaPhotoImageBackground item)
{
if (Background is not null)
{
item.IsUniversalBackground = Background.IsUniversalBackground;
item.FirstGradientColor = Background.FirstGradientColor;
item.SecondGradientColor = Background.SecondGradientColor;
}
}
}

View File

@@ -0,0 +1,47 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using WatchIt.Database.Model.Media;
namespace WatchIt.Common.Model.Media;
public class MediaPhotoResponse : MediaPhoto
{
#region PROPERTIES
[JsonPropertyName("id")]
public Guid Id { get; set; }
[JsonPropertyName("upload_date")]
public DateTime UploadDate { get; set; }
#endregion
#region CONSTRUCTORS
[JsonConstructor]
public MediaPhotoResponse() {}
[SetsRequiredMembers]
public MediaPhotoResponse(MediaPhotoImage mediaPhotoImage)
{
Id = mediaPhotoImage.Id;
MediaId = mediaPhotoImage.MediaId;
Image = mediaPhotoImage.Image;
MimeType = mediaPhotoImage.MimeType;
UploadDate = mediaPhotoImage.UploadDate;
if (mediaPhotoImage.MediaPhotoImageBackground is not null)
{
Background = new MediaPhotoBackground
{
IsUniversalBackground = mediaPhotoImage.MediaPhotoImageBackground.IsUniversalBackground,
FirstGradientColor = mediaPhotoImage.MediaPhotoImageBackground.FirstGradientColor,
SecondGradientColor = mediaPhotoImage.MediaPhotoImageBackground.SecondGradientColor
};
}
}
#endregion
}

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using WatchIt.Common.Query;
namespace WatchIt.Common.Model.Movies;

View File

@@ -6,7 +6,7 @@ public class MovieRequest : Movie
{
#region PUBLIC METHODS
public Media CreateMedia() => new Media
public Database.Model.Media.Media CreateMedia() => new Database.Model.Media.Media
{
Title = Title,
OriginalTitle = OriginalTitle,
@@ -21,7 +21,7 @@ public class MovieRequest : Movie
Budget = Budget,
};
public void UpdateMedia(Media media)
public void UpdateMedia(Database.Model.Media.Media media)
{
media.Title = Title;
media.OriginalTitle = OriginalTitle;

View File

@@ -8,6 +8,7 @@
<ItemGroup>
<ProjectReference Include="..\..\WatchIt.Database\WatchIt.Database.Model\WatchIt.Database.Model\WatchIt.Database.Model.csproj" />
<ProjectReference Include="..\WatchIt.Common.Query\WatchIt.Common.Query.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,11 +1,12 @@
using System.Reflection;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc;
namespace WatchIt.Common.Model;
namespace WatchIt.Common.Query;
public abstract class QueryParameters<T> where T : class
public abstract class QueryParameters
{
#region PROPERTIES
@@ -22,9 +23,93 @@ public abstract class QueryParameters<T> where T : class
public int? After { get; set; }
#endregion
#region PUBLIC METHODS
public override string ToString()
{
List<string> queries = new List<string>();
PropertyInfo[] properties = this.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
object? value = property.GetValue(this);
FromQueryAttribute? attribute = property.GetCustomAttributes<FromQueryAttribute>(true).FirstOrDefault();
if (value is not null && attribute is not null)
{
string query = $"{attribute.Name}={value}";
queries.Add(query);
}
}
return $"?{string.Join('&', queries)}";
}
#endregion
#region PRIVATE METHODS
protected static bool TestBoolean(bool property, bool? query) =>
(
query is null
||
property == query
);
protected static bool TestString(string? property, string? regexQuery) =>
(
string.IsNullOrEmpty(regexQuery)
||
(
!string.IsNullOrEmpty(property)
&&
new Regex(regexQuery).IsMatch(property)
)
);
protected static bool TestComparable(IComparable? property, IComparable? exact, IComparable? from, IComparable? to) =>
(
(
exact is null
||
(
property is not null
&&
property.CompareTo(exact) == 0
)
)
&&
(
from is null
||
(
property is not null
&&
property.CompareTo(from) > 0
)
)
&&
(
to is null
||
(
property is not null
&&
property.CompareTo(to) < 0
)
)
);
#endregion
}
public abstract class QueryParameters<T> : QueryParameters where T : class
{
#region PUBLIC METHODS
public abstract bool IsMeetingConditions(T item);
@@ -65,54 +150,4 @@ public abstract class QueryParameters<T> where T : class
}
#endregion
#region PRIVATE METHODS
protected bool TestString(string? property, string? regexQuery) =>
(
string.IsNullOrEmpty(regexQuery)
||
(
!string.IsNullOrEmpty(property)
&&
new Regex(regexQuery).IsMatch(property)
)
);
protected bool TestComparable(IComparable? property, IComparable? exact, IComparable? from, IComparable? to) =>
(
(
exact is null
||
(
property is not null
&&
property.CompareTo(exact) == 0
)
)
&&
(
from is null
||
(
property is not null
&&
property.CompareTo(from) > 0
)
)
&&
(
to is null
||
(
property is not null
&&
property.CompareTo(to) < 0
)
)
);
#endregion
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,43 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace WatchIt.Common.Services.HttpClient;
public class HttpClientService(System.Net.Http.HttpClient httpClient) : IHttpClientService
{
#region PUBLIC METHODS
public async Task<HttpResponse> SendRequestAsync(HttpRequest request)
{
HttpMethod method = request.MethodType switch
{
HttpMethodType.Get => HttpMethod.Get,
HttpMethodType.Post => HttpMethod.Post,
HttpMethodType.Put => HttpMethod.Put,
HttpMethodType.Patch => HttpMethod.Patch,
HttpMethodType.Delete => HttpMethod.Delete,
_ => throw new ArgumentOutOfRangeException()
};
HttpRequestMessage httpRequest = new HttpRequestMessage(method, request.FullUrl);
if (request.Body is not null)
{
string json = JsonSerializer.Serialize(request.Body);
HttpContent content = new StringContent(json);
content.Headers.ContentType!.MediaType = "application/json";
httpRequest.Content = content;
}
foreach (KeyValuePair<string, string> header in request.Headers)
{
httpRequest.Headers.Add(header.Key, header.Value);
}
HttpResponseMessage response = await httpClient.SendAsync(httpRequest);
return new HttpResponse(response);
}
#endregion
}

View File

@@ -0,0 +1,10 @@
namespace WatchIt.Common.Services.HttpClient;
public enum HttpMethodType
{
Get,
Post,
Put,
Patch,
Delete
}

View File

@@ -0,0 +1,32 @@
using System.Diagnostics.CodeAnalysis;
using WatchIt.Common.Query;
namespace WatchIt.Common.Services.HttpClient;
public class HttpRequest
{
#region PROPERTIES
public required HttpMethodType MethodType { get; set; }
public required string Url { get; set; }
public QueryParameters? Query { get; set; }
public object? Body { get; set; }
public Dictionary<string, string> Headers { get; } = new Dictionary<string, string>();
public string FullUrl => $"{Url}{(Query is not null ? Query.ToString() : string.Empty)}";
#endregion
#region CONSTRUCTORS
[SetsRequiredMembers]
public HttpRequest(HttpMethodType methodType, string url)
{
MethodType = methodType;
Url = url;
}
#endregion
}

View File

@@ -0,0 +1,107 @@
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
namespace WatchIt.Common.Services.HttpClient;
public class HttpResponse
{
#region FIELDS
private HttpResponseMessage _message;
private Action? _2XXAction;
private Action? _400Action;
private Action? _401Action;
private Action? _403Action;
private Action? _404Action;
#endregion
#region CONSTRUCTORS
internal HttpResponse(HttpResponseMessage message)
{
_message = message;
}
#endregion
#region PUBLIC METHODS
public HttpResponse RegisterActionFor2XXSuccess(Action action)
{
_2XXAction = action;
return this;
}
public HttpResponse RegisterActionFor2XXSuccess<T>(Action<T> action)
{
_2XXAction = () => Invoke(action);
return this;
}
public HttpResponse RegisterActionFor400BadRequest(Action<IDictionary<string, string[]>> action)
{
_400Action = () => Invoke(action);
return this;
}
public HttpResponse RegisterActionFor401Unauthorized(Action action)
{
_401Action = action;
return this;
}
public HttpResponse RegisterActionFor403Forbidden(Action action)
{
_403Action = action;
return this;
}
public HttpResponse RegisterActionFor404NotFound(Action action)
{
_404Action = action;
return this;
}
public void ExecuteAction()
{
switch ((int)_message.StatusCode)
{
case >= 200 and <= 299: _2XXAction?.Invoke(); break;
case 400: _400Action?.Invoke(); break;
case 401: _401Action?.Invoke(); break;
case 403: _403Action?.Invoke(); break;
case 404: _404Action?.Invoke(); break;
}
}
#endregion
#region PRIVATE METHODS
private async void Invoke<T>(Action<T> action)
{
Stream streamData = await _message.Content.ReadAsStreamAsync();
T? data = await JsonSerializer.DeserializeAsync<T>(streamData);
action.Invoke(data!);
}
private async void Invoke(Action<IDictionary<string, string[]>> action)
{
Stream streamData = await _message.Content.ReadAsStreamAsync();
ValidationProblemDetails? data = await JsonSerializer.DeserializeAsync<ValidationProblemDetails>(streamData, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
});
action.Invoke(data!.Errors);
}
#endregion
}

View File

@@ -0,0 +1,6 @@
namespace WatchIt.Common.Services.HttpClient;
public interface IHttpClientService
{
Task<HttpResponse> SendRequestAsync(HttpRequest request);
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\WatchIt.Common.Query\WatchIt.Common.Query.csproj" />
</ItemGroup>
</Project>