Added backend functionality

This commit is contained in:
2025-03-01 16:57:18 +01:00
parent 55be2f1e88
commit a1ba2f9571
19 changed files with 427 additions and 11 deletions

View File

@@ -0,0 +1,28 @@
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

@@ -0,0 +1,10 @@
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,7 +1,15 @@
namespace WorkTime.Api.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public class TimeEntry {
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; }

View File

@@ -1,15 +1,35 @@
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

@@ -0,0 +1,14 @@
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

@@ -0,0 +1,37 @@
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

@@ -8,7 +8,9 @@
</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>
@@ -17,4 +19,8 @@
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\WorkTime.Defaults\WorkTime.Defaults.csproj" />
</ItemGroup>
</Project>