blob: 6cdae3b4c0a9838d97f80837dbfa43850905e7d3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
}
}
}
|