summaryrefslogtreecommitdiff
path: root/Logics/RafflePlayLogic.cs
blob: 026bb919418b68fd526b6d67078f2776bb6e84a2 (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
using BackendPIA.Models;
using BackendPIA.Services;

namespace BackendPIA.Logics {
    public class RafflePlayLogic {
        private readonly IGameService _game_service;
        private readonly IRaffleService _raffle_service;
        private readonly long _raffle_id;
        public string? ErrorMessage { get; set; }
        public IEnumerable<RaffleWinner>? Winners { get; set;}

        public RafflePlayLogic(IGameService game_service, IRaffleService raffle_service, long raffle_id) {
            _game_service = game_service;
            _raffle_service = raffle_service;
            _raffle_id = raffle_id;
        }

        public async Task<bool> Call() {
            var raffle = await _raffle_service.GetRaffle(_raffle_id);

            // Checks.
            if(raffle == null) {
                ErrorMessage = $"The raffle with id {_raffle_id} couldn't be found.";

                return false;
            }

            if(raffle.Winners >= _raffle_service.GetRaffleTickets(_raffle_id).Count()) {
                ErrorMessage = $"Can't play: not enough players.";

                return false;
            }

            Winners = await _game_service.GetWinners(_raffle_id);

            return true;
        }
    }
}