Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,5 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml

/Backend/backend.wwwapi/appsettings.json
22 changes: 22 additions & 0 deletions Backend/backend.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "backend.wwwapi", "backend.wwwapi\backend.wwwapi.csproj", "{C704BBDE-F92C-4184-9758-C656FC6FF4D2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C704BBDE-F92C-4184-9758-C656FC6FF4D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C704BBDE-F92C-4184-9758-C656FC6FF4D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C704BBDE-F92C-4184-9758-C656FC6FF4D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C704BBDE-F92C-4184-9758-C656FC6FF4D2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions Backend/backend.wwwapi/DataContext/CatContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using backend.wwwapi.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;

namespace backend.wwwapi.DataContext
{
public class CatContext : DbContext
{
public CatContext(DbContextOptions<CatContext> options) : base(options) { }

public DbSet<Cat> Cats { get; set; }
}
}
107 changes: 107 additions & 0 deletions Backend/backend.wwwapi/Endpoints/CatAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using backend.wwwapi.Models;
using backend.wwwapi.Repository;

namespace backend.wwwapi.Endpoints
{
public static class CatAPI
{
public static void ConfigureCatAPI(this WebApplication app)
{
app.MapGet("/", GetIndex);
var cats = app.MapGroup("cats");
cats.MapGet("/", GetCats);
cats.MapPost("/", AddCat);
cats.MapPut("/{id}", UpdateCat);
cats.MapDelete("/{id}", DeleteCat);
}

private static async Task<IResult> GetIndex(IDatabaseRepository<Cat> repository)
{
try
{
return Results.Ok("Working");
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
}

private static async Task<IResult> GetCats(IDatabaseRepository<Cat> repository)
{
try
{
return Results.Ok(repository.GetAll());
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
}

private static async Task<IResult> AddCat(Cat cat, IDatabaseRepository<Cat> repository)
{
try
{
if (cat == null)
{
return Results.BadRequest("Invalid Cat.");
}

repository.Insert(cat);
return Results.Created($"/cats/{cat.Id}", cat);
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
}

private static async Task<IResult> UpdateCat(int id, Cat updatedCat, IDatabaseRepository<Cat> repository)
{
try
{
// Find the existing Cat
var existingCat = repository.GetById(id);
if (existingCat == null)
{
return Results.NotFound($"Cat with Id {id} not found.");
}

// Update the fields
existingCat.Rating = updatedCat.Rating;

// Save changes
repository.Update(existingCat);

return Results.Ok(existingCat);
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
}

private static async Task<IResult> DeleteCat(int id, IDatabaseRepository<Cat> repository)
{
try
{
// Find the Cat by Id
var existingCat = repository.GetById(id);
if (existingCat == null)
{
return Results.NotFound($"Cat with Id {id} not found.");
}

// Delete the Cat
repository.Delete(id);

return Results.Ok(existingCat);
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
}
}
}
52 changes: 52 additions & 0 deletions Backend/backend.wwwapi/Migrations/20241017124826_First.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions Backend/backend.wwwapi/Migrations/20241017124826_First.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace backend.wwwapi.Migrations
{
/// <inheritdoc />
public partial class First : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Cats",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "text", nullable: false),
Age = table.Column<int>(type: "integer", nullable: false),
Rating = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Cats", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Cats");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Backend/backend.wwwapi/Migrations/20241017142511_Second.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace backend.wwwapi.Migrations
{
/// <inheritdoc />
public partial class Second : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{

}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{

}
}
}
49 changes: 49 additions & 0 deletions Backend/backend.wwwapi/Migrations/CatContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using backend.wwwapi.DataContext;

#nullable disable

namespace backend.wwwapi.Migrations
{
[DbContext(typeof(CatContext))]
partial class CatContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("backend.wwwapi.Models.Cat", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");

NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));

b.Property<int>("Age")
.HasColumnType("integer");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");

b.Property<int>("Rating")
.HasColumnType("integer");

b.HasKey("Id");

b.ToTable("Cats");
});
#pragma warning restore 612, 618
}
}
}
10 changes: 10 additions & 0 deletions Backend/backend.wwwapi/Models/Cat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace backend.wwwapi.Models
{
public class Cat
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Rating { get; set; }
}
}
Loading