diff options
Diffstat (limited to 'Services/RaffleService.cs')
-rw-r--r-- | Services/RaffleService.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/Services/RaffleService.cs b/Services/RaffleService.cs new file mode 100644 index 0000000..6cdae3b --- /dev/null +++ b/Services/RaffleService.cs @@ -0,0 +1,61 @@ +using Microsoft.EntityFrameworkCore; +using BackendPIA.Models; + +namespace BackendPIA.Services { + public class RaffleService : IRaffleService { + private readonly ApplicationDbContext _context; + + public RaffleService(ApplicationDbContext context) { + _context = context; + } + + public async Task<Raffle> CreateRaffle(Raffle to_create) { + to_create.IsClosed = false; + + await _context.AddAsync(to_create); + await _context.SaveChangesAsync(); + + return to_create; + } + + public async Task<Raffle> UpdateRaffle(Raffle to_update) { + bool it_exists = _context.Raffles.Any(r => r.Id == to_update.Id); + + if(!it_exists) + return null; + + _context.Update(to_update); + await _context.SaveChangesAsync(); + + return to_update; + } + + public async Task<IEnumerable<Raffle>> GetRaffles(string query) { + if(String.IsNullOrEmpty(query)) + return await _context.Raffles.ToListAsync(); + + return _context.Raffles.Where(r => r.Name == query); + } + + public async Task<Raffle> GetRaffle(long id) { + var raffle = await _context.Raffles.FindAsync(id); + + if(raffle == null) + return null; + + return raffle; + } + + public async Task<bool> DeleteRaffle(long id) { + var to_delete = await _context.Raffles.FindAsync(id); + + if(to_delete == null) + return false; + + _context.Raffles.Remove(to_delete); + await _context.SaveChangesAsync(); + + return true; + } + } +}
\ No newline at end of file |