-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiceController.cs
35 lines (29 loc) · 931 Bytes
/
DiceController.cs
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
using System.Net;
using Microsoft.AspNetCore.Mvc;
public class DiceController: ControllerBase
{
private ILogger<DiceController> logger;
public DiceController(ILogger<DiceController> logger)
{
this.logger = logger;
}
[HttpGet("/rolldice")]
public List<int> RollDice(string player, int? rolls)
{
if(!rolls.HasValue)
{
logger.LogError("Missing rolls parameter");
throw new HttpRequestException("Missing rolls parameter", null, HttpStatusCode.BadRequest);
}
var result = new Dice(1, 6).rollTheDice(rolls.Value);
if (string.IsNullOrEmpty(player))
{
logger.LogInformation("Anonymous player rolled a dice with results: {result}", result);
}
else
{
logger.LogInformation("{player} rolled a dice with results: {result}", player, result);
}
return result;
}
}