summaryrefslogtreecommitdiff
path: root/Services
diff options
context:
space:
mode:
Diffstat (limited to 'Services')
-rw-r--r--Services/IRaffleService.cs11
-rw-r--r--Services/ITokenGenerator.cs1
-rw-r--r--Services/RaffleService.cs61
3 files changed, 72 insertions, 1 deletions
diff --git a/Services/IRaffleService.cs b/Services/IRaffleService.cs
new file mode 100644
index 0000000..2326629
--- /dev/null
+++ b/Services/IRaffleService.cs
@@ -0,0 +1,11 @@
+using BackendPIA.Models;
+
+namespace BackendPIA.Services {
+ public interface IRaffleService {
+ public Task<Raffle> CreateRaffle(Raffle to_create);
+ public Task<Raffle> UpdateRaffle(Raffle to_update);
+ public Task<IEnumerable<Raffle>> GetRaffles(string query);
+ public Task<Raffle> GetRaffle(long id);
+ public Task<bool> DeleteRaffle(long id);
+ }
+} \ No newline at end of file
diff --git a/Services/ITokenGenerator.cs b/Services/ITokenGenerator.cs
index 32db2b6..4a66029 100644
--- a/Services/ITokenGenerator.cs
+++ b/Services/ITokenGenerator.cs
@@ -1,4 +1,3 @@
-using System.Security.Claims;
using BackendPIA.Models;
namespace BackendPIA.Services {
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