Removed API project and restructured solution

This commit is contained in:
2025-03-04 10:48:04 +01:00
parent a1ba2f9571
commit a98709b0a1
70 changed files with 7 additions and 344 deletions

View File

@@ -1,28 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using WorkTime.Api.Models;
using WorkTime.Api.Services;
namespace WorkTime.Api.Controller;
[ApiController, Route("entries")]
public class EntryController(ITimeEntryService entryService) : ControllerBase {
[HttpGet("{id:guid}")]
public async Task<IResult> GetEntries(Guid id) {
var result = await entryService.GetTimeEntries(id);
return result.HttpResult;
}
[HttpPost("{id:guid}")]
public async Task<IResult> AddEntry(Guid id, TimeEntryDto entry) {
var result = await entryService.AddTimeEntry(id, entry);
return result.HttpResult;
}
[HttpDelete("{id:int}")]
public async Task<IResult> DeleteEntry(int id) {
var result = await entryService.RemoveTimeEntry(id);
return result.HttpResult;
}
}

View File

@@ -1,10 +0,0 @@
using Microsoft.EntityFrameworkCore;
using WorkTime.Api.Models;
namespace WorkTime.Api;
public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options) {
public DbSet<TimeEntry> Entries { get; set; }
}

View File

@@ -1,23 +0,0 @@
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["src/WorkTime.Api/WorkTime.Api.csproj", "src/WorkTime.Api/"]
RUN dotnet restore "src/WorkTime.Api/WorkTime.Api.csproj"
COPY . .
WORKDIR "/src/src/WorkTime.Api"
RUN dotnet build "WorkTime.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "WorkTime.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WorkTime.Api.dll"]

View File

@@ -1,23 +0,0 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WorkTime.Api.Models;
public class TimeEntry : TimeEntryDto {
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int EntryId { get; set; }
public Guid Owner { get; set; }
}
public class TimeEntryDto {
public DateTime RegisteredAt { get; set; }
public EntryType Type { get; set; }
public bool IsMoba { get; set; }
}
public enum EntryType {
Login = 0,
Logout = 1,
StartDrive = 2,
EndDrive = 3
}

View File

@@ -1,35 +0,0 @@
using Scalar.AspNetCore;
using WorkTime.Api;
using WorkTime.Api.Services;
using WorkTime.Api.Services.Implementation;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.AddServiceDefaults();
builder.Services.AddControllers();
builder.Services.AddProblemDetails();
builder.Services.AddNpgsql<DatabaseContext>(builder.Configuration.GetConnectionString("data"));
builder.Services.AddScoped<ITimeEntryService, TimeEntryService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
app.MapOpenApi();
app.MapScalarApiReference(options => {
options.Servers = [];
});
await app.Services
.CreateAsyncScope().ServiceProvider
.GetRequiredService<DatabaseContext>()
.Database.EnsureCreatedAsync();
}
app.UseHttpsRedirection();
app.MapControllers();
app.Run();

View File

@@ -1,23 +0,0 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5295",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7294;http://localhost:5295",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -1,14 +0,0 @@
using WorkTime.Api.Models;
using WorkTime.Defaults;
namespace WorkTime.Api.Services;
public interface ITimeEntryService {
public Task<DataResult<IEnumerable<TimeEntry>>> GetTimeEntries(Guid owner);
public Task<DataResult> AddTimeEntry(Guid owner, TimeEntryDto entry);
public Task<DataResult<bool, IResult>> RemoveTimeEntry(int id);
}

View File

@@ -1,37 +0,0 @@
using Microsoft.EntityFrameworkCore;
using WorkTime.Api.Models;
using WorkTime.Defaults;
namespace WorkTime.Api.Services.Implementation;
public class TimeEntryService(DatabaseContext context) : ITimeEntryService {
public async Task<DataResult<IEnumerable<TimeEntry>>> GetTimeEntries(Guid owner) {
return await context.Entries
.Where(entry => entry.Owner == owner)
.ToArrayAsync();
}
public async Task<DataResult> AddTimeEntry(Guid owner, TimeEntryDto entry) {
var dbEntry = new TimeEntry {
Owner = owner,
RegisteredAt = entry.RegisteredAt,
Type = entry.Type,
IsMoba = entry.IsMoba
};
await context.Entries.AddAsync(dbEntry);
await context.SaveChangesAsync();
return true;
}
public async Task<DataResult<bool, IResult>> RemoveTimeEntry(int id) {
var entry = await context.Entries.FindAsync(id);
if (entry is null)
return TypedResults.NotFound();
context.Entries.Remove(entry);
await context.SaveChangesAsync();
return true;
}
}

View File

@@ -1,26 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.1.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0"/>
<PackageReference Include="Scalar.AspNetCore" Version="2.0.20" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\WorkTime.Defaults\WorkTime.Defaults.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,8 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -1,9 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}