diff --git a/api/src/Data/AppDbContext.cs b/api/src/Data/AppDbContext.cs new file mode 100644 index 0000000..49df799 --- /dev/null +++ b/api/src/Data/AppDbContext.cs @@ -0,0 +1,13 @@ + +using agologumApi.Models; + +using Microsoft.EntityFrameworkCore; + +public class AppDbContext : DbContext { + + public AppDbContext(DbContextOptions options) : base(options) { + + } + + public DbSet Users { get; set; } +} \ No newline at end of file diff --git a/api/src/Services/UserService.cs b/api/src/Services/UserService.cs new file mode 100644 index 0000000..0b085ec --- /dev/null +++ b/api/src/Services/UserService.cs @@ -0,0 +1,47 @@ + +using Microsoft.EntityFrameworkCore; + +using agologumApi.Models; + +namespace agologumApi.Services; + +public class UserService { + + private readonly AppDbContext db_; + + public UserService(AppDbContext db) { + db_ = db; + } + + public async Task> GetAll() { + return await db_.Users.ToListAsync(); + } + + public async Task Get(int id) { + return await db_.Users.FindAsync(id); + } + + public async Task Create(User user) { + db_.Users.Add(user); + await db_.SaveChangesAsync(); + return user; + } + + public async Task Update(User user) { + db_.Users.Update(user); + await db_.SaveChangesAsync(); + return user; + } + + public async Task Delete(int id) { + User? user = await db_.Users.FindAsync(id); + if(user != null) { + db_.Users.Remove(user); + await db_.SaveChangesAsync(); + return true; + } else { + return false; + } + } + +} \ No newline at end of file