Updated lots of stuff, got multi scrape working, need to test not-nullable chapter novel ids with our current model, now supports sqlite and postgres concurrently (and easy add more), need to get it deployed/do auth
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
83
Treestar.Shared/AccessLayers/ApiAccessLayer.cs
Normal file
83
Treestar.Shared/AccessLayers/ApiAccessLayer.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System.Net.Mime;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using Newtonsoft.Json;
|
||||
using Treestar.Shared.Models;
|
||||
|
||||
namespace Treestar.Shared.AccessLayers;
|
||||
|
||||
public abstract class ApiAccessLayer
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
protected ApiAccessLayer(string apiBaseUrl)
|
||||
{
|
||||
var handler = new HttpClientHandler()
|
||||
{
|
||||
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
|
||||
};
|
||||
_httpClient = new HttpClient(handler);
|
||||
_httpClient.BaseAddress = new Uri(apiBaseUrl, UriKind.Absolute);
|
||||
}
|
||||
|
||||
private async Task<HttpResponseWrapper> SendRequest(HttpRequestMessage message)
|
||||
{
|
||||
var response = await _httpClient.SendAsync(message);
|
||||
return new HttpResponseWrapper()
|
||||
{
|
||||
HttpResponseMessage = response
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<HttpResponseWrapper<T>> SendRequest<T>(HttpRequestMessage message)
|
||||
{
|
||||
var wrapper = await SendRequest(message);
|
||||
if (wrapper.HttpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
var parsedJson =
|
||||
JsonConvert.DeserializeObject<T>(await wrapper.HttpResponseMessage.Content.ReadAsStringAsync());
|
||||
return new HttpResponseWrapper<T>
|
||||
{
|
||||
HttpResponseMessage = wrapper.HttpResponseMessage,
|
||||
ResponseObject = parsedJson
|
||||
};
|
||||
}
|
||||
return new HttpResponseWrapper<T>
|
||||
{
|
||||
HttpResponseMessage = wrapper.HttpResponseMessage,
|
||||
ResponseObject = default(T)
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
protected HttpRequestMessage CreateRequestMessage(string endpoint, HttpMethod method, Dictionary<string, string>? queryParams = null,
|
||||
object? data = null)
|
||||
{
|
||||
HttpRequestMessage message = new HttpRequestMessage();
|
||||
string uri = endpoint;
|
||||
if (queryParams != null)
|
||||
{
|
||||
uri = QueryHelpers.AddQueryString(endpoint, queryParams);
|
||||
}
|
||||
message.RequestUri = new Uri(uri, UriKind.Relative);
|
||||
message.Method = method;
|
||||
if (data != null)
|
||||
{
|
||||
message.Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, MediaTypeNames.Application.Json);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
protected async Task<HttpResponseWrapper> SendRequest(string endpoint, HttpMethod method, Dictionary<string, string>? queryParams = null, object? data = null)
|
||||
{
|
||||
HttpRequestMessage message = CreateRequestMessage(endpoint, method, queryParams, data);
|
||||
return await SendRequest(message);
|
||||
}
|
||||
|
||||
protected async Task<HttpResponseWrapper<T>> SendRequest<T>(string endpoint, HttpMethod method, Dictionary<string, string>? queryParams = null, object? data = null)
|
||||
{
|
||||
HttpRequestMessage message = CreateRequestMessage(endpoint, method, queryParams, data);
|
||||
return await SendRequest<T>(message);
|
||||
}
|
||||
}
|
||||
32
Treestar.Shared/Models/DBDomain/Author.cs
Normal file
32
Treestar.Shared/Models/DBDomain/Author.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Treestar.Shared.Models.DBDomain
|
||||
{
|
||||
public class Author : BaseEntity
|
||||
{
|
||||
[Key]
|
||||
public string Url { get; set; }
|
||||
public string Name { get; set; }
|
||||
[JsonIgnore]
|
||||
public List<Novel> Novels { get; set; }
|
||||
|
||||
protected bool Equals(Author other)
|
||||
{
|
||||
return Url == other.Url;
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
if (obj.GetType() != this.GetType()) return false;
|
||||
return Equals((Author) obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Url.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Treestar.Shared/Models/DBDomain/BaseEntity.cs
Normal file
8
Treestar.Shared/Models/DBDomain/BaseEntity.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Treestar.Shared.Models.DBDomain
|
||||
{
|
||||
public abstract class BaseEntity
|
||||
{
|
||||
public DateTime DateCreated { get; set; }
|
||||
public DateTime DateModified { get; set; }
|
||||
}
|
||||
}
|
||||
37
Treestar.Shared/Models/DBDomain/Chapter.cs
Normal file
37
Treestar.Shared/Models/DBDomain/Chapter.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Treestar.Shared.Models.DBDomain
|
||||
{
|
||||
public class Chapter : BaseEntity
|
||||
{
|
||||
public int ChapterNumber { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string? Content { get; set; }
|
||||
public string? RawContent { get; set; }
|
||||
[Key]
|
||||
public string Url { get; set; }
|
||||
public DateTime? DatePosted { get; set; }
|
||||
public DateTime? DateUpdated { get; set; }
|
||||
public DateTime? LastContentFetch { get; set; }
|
||||
[Required]
|
||||
public string NovelUrl { get; set; }
|
||||
|
||||
protected bool Equals(Chapter other)
|
||||
{
|
||||
return Url == other.Url;
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
if (obj.GetType() != this.GetType()) return false;
|
||||
return Equals((Chapter) obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Url.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Treestar.Shared/Models/DBDomain/Novel.cs
Normal file
21
Treestar.Shared/Models/DBDomain/Novel.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Treestar.Shared.Models.Enums;
|
||||
|
||||
namespace Treestar.Shared.Models.DBDomain
|
||||
{
|
||||
[Index(nameof(Guid))]
|
||||
public class Novel : BaseEntity
|
||||
{
|
||||
[Key]
|
||||
public string Url { get; set; }
|
||||
public Guid Guid { get; set; }
|
||||
public string Title { get; set; }
|
||||
public Author? Author { get; set; }
|
||||
public List<Tag> Tags { get; set; }
|
||||
public List<Chapter> Chapters { get; set; }
|
||||
public NovelStatus Status { get; set; }
|
||||
public DateTime LastUpdated { get; set; }
|
||||
public DateTime DatePosted { get; set; }
|
||||
}
|
||||
}
|
||||
31
Treestar.Shared/Models/DBDomain/Tag.cs
Normal file
31
Treestar.Shared/Models/DBDomain/Tag.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Treestar.Shared.Models.DBDomain
|
||||
{
|
||||
public class Tag : BaseEntity
|
||||
{
|
||||
[Key]
|
||||
public string TagValue { get; set; }
|
||||
[JsonIgnore]
|
||||
public List<Novel> Novels { get; set; }
|
||||
|
||||
protected bool Equals(Tag other)
|
||||
{
|
||||
return TagValue == other.TagValue;
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
if (obj.GetType() != this.GetType()) return false;
|
||||
return Equals((Tag) obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return TagValue.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Treestar.Shared/Models/DBDomain/User.cs
Normal file
12
Treestar.Shared/Models/DBDomain/User.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Treestar.Shared.Models.DBDomain
|
||||
{
|
||||
public class User : BaseEntity
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
public string Email { get; set; }
|
||||
public List<UserNovel> WatchedNovels { get; set; }
|
||||
}
|
||||
}
|
||||
14
Treestar.Shared/Models/DBDomain/UserNovel.cs
Normal file
14
Treestar.Shared/Models/DBDomain/UserNovel.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Treestar.Shared.Models.DBDomain
|
||||
{
|
||||
public class UserNovel
|
||||
{
|
||||
[JsonIgnore]
|
||||
public int UserId { get; set; }
|
||||
public string NovelUrl { get; set; }
|
||||
public Novel Novel { get; set; }
|
||||
public User User { get; set; }
|
||||
public int LastChapterRead { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Treestar.Shared.Models.DTO.Requests;
|
||||
|
||||
public class ScrapeNovelRequest
|
||||
{
|
||||
public string NovelUrl { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Treestar.Shared.Models.DTO.Requests;
|
||||
|
||||
public class ScrapeNovelsRequest
|
||||
{
|
||||
[JsonProperty("novelUrls")]
|
||||
public List<string> NovelUrls { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using Treestar.Shared.Models.DBDomain;
|
||||
|
||||
namespace Treestar.Shared.Models.DTO.Responses;
|
||||
|
||||
public class ScrapeNovelsResponse
|
||||
{
|
||||
public List<Novel> SuccessfulNovels { get; set; }
|
||||
public Dictionary<string, Exception> Failures { get; set; }
|
||||
}
|
||||
9
Treestar.Shared/Models/Enums/NovelStatus.cs
Normal file
9
Treestar.Shared/Models/Enums/NovelStatus.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Treestar.Shared.Models.Enums;
|
||||
|
||||
public enum NovelStatus
|
||||
{
|
||||
Unknown,
|
||||
InProgress,
|
||||
Completed,
|
||||
Hiatus
|
||||
}
|
||||
11
Treestar.Shared/Models/HttpResponseWrapper.cs
Normal file
11
Treestar.Shared/Models/HttpResponseWrapper.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Treestar.Shared.Models;
|
||||
|
||||
public class HttpResponseWrapper<T> : HttpResponseWrapper
|
||||
{
|
||||
public T? ResponseObject { get; set; }
|
||||
}
|
||||
|
||||
public class HttpResponseWrapper
|
||||
{
|
||||
public HttpResponseMessage HttpResponseMessage { get; set; }
|
||||
}
|
||||
20
Treestar.Shared/Treestar.Shared.csproj
Normal file
20
Treestar.Shared/Treestar.Shared.csproj
Normal file
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Interfaces" />
|
||||
<Folder Include="Utility" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.7" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user