FEAT: DB basics
This commit is contained in:
18
TicketAppIncrArchi.Infrastructure/DbContext.cs
Normal file
18
TicketAppIncrArchi.Infrastructure/DbContext.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace TicketAppIncrArchi.Infrastructure.Persistence;
|
||||
|
||||
using TicketAppIncrArchi.Domain.Entities;
|
||||
|
||||
public class AppDbContext : DbContext
|
||||
{
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DbSet <Ticket> Tickets {get;set;}
|
||||
}
|
||||
|
||||
37
TicketAppIncrArchi.Infrastructure/TicketRepository.cs
Normal file
37
TicketAppIncrArchi.Infrastructure/TicketRepository.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TicketAppIncrArchi.Domain.Entities;
|
||||
using TicketAppIncrArchi.Application.Interfaces;
|
||||
using TicketAppIncrArchi.Infrastructure.Persistence;
|
||||
|
||||
|
||||
public class TicketRepository : ITicketRepository
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public TicketRepository(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Ticket?> GetByIdAsync(Guid id)
|
||||
=> await _context.Tickets.FindAsync(id);
|
||||
|
||||
public async Task<List<Ticket>> GetAllAsync()
|
||||
=> await _context.Tickets.ToListAsync();
|
||||
|
||||
public async Task<Ticket> AddAsync(Ticket ticket)
|
||||
{
|
||||
_context.Tickets.Add(ticket);
|
||||
await _context.SaveChangesAsync();
|
||||
return(ticket);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id)
|
||||
{
|
||||
var ticket = await _context.Tickets.FindAsync(id);
|
||||
if (ticket is null) return;
|
||||
|
||||
_context.Tickets.Remove(ticket);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user