Slide 12
Slide 12 text
namespace DoggoApi.Controllers
{
[ApiController]
[Route("api/[controller]")]
[Authorize("access:api")]
public class DoggosController : ControllerBase
{
[HttpGet(Name = nameof(GetAllDoggos))]
[AllowAnonymous]
public ActionResult GetAllDoggos()
{
List doggos = _repository.GetAll().ToList();
return Ok(_mapper.Map(doggos));
}
[HttpGet]
[Route("{id:guid}", Name = nameof(GetSingleDoggo))]
public ActionResult GetSingleDoggo(Guid id)
{
DoggoEntity? entity = _repository.GetSingle(id);
if (entity == null)
{
return NotFound();
}
DoggoDto item = _mapper.Map(entity);
return Ok(item);
}
[HttpPost(Name = nameof(AddDoggo))]
public async Task> AddDoggo(
[FromBody] DoggoCreateDto createDto)
{
if (createDto == null)
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
[Authorize("access:api")]
namespace DoggoApi.Controllers
1
{
2
[ApiController]
3
[Route("api/[controller]")]
4
5
public class DoggosController : ControllerBase
6
{
7
[HttpGet(Name = nameof(GetAllDoggos))]
8
[AllowAnonymous]
9
public ActionResult GetAllDoggos()
10
{
11
List doggos = _repository.GetAll().ToList();
12
13
return Ok(_mapper.Map(doggos));
14
}
15
16
[HttpGet]
17
[Route("{id:guid}", Name = nameof(GetSingleDoggo))]
18
public ActionResult GetSingleDoggo(Guid id)
19
{
20
DoggoEntity? entity = _repository.GetSingle(id);
21
22
if (entity == null)
23
{
24
return NotFound();
25
}
26
27
DoggoDto item = _mapper.Map(entity);
28
29
return Ok(item);
30
}
31
32
[HttpPost(Name = nameof(AddDoggo))]
33
public async Task> AddDoggo(
34
[FromBody] DoggoCreateDto createDto)
35
{
36
if (createDto == null)
37
[HttpGet(Name = nameof(GetAllDoggos))]
[AllowAnonymous]
public ActionResult GetAllDoggos()
{
List doggos = _repository.GetAll().ToList();
return Ok(_mapper.Map(doggos));
}
namespace DoggoApi.Controllers
1
{
2
[ApiController]
3
[Route("api/[controller]")]
4
[Authorize("access:api")]
5
public class DoggosController : ControllerBase
6
{
7
8
9
10
11
12
13
14
15
16
[HttpGet]
17
[Route("{id:guid}", Name = nameof(GetSingleDoggo))]
18
public ActionResult GetSingleDoggo(Guid id)
19
{
20
DoggoEntity? entity = _repository.GetSingle(id);
21
22
if (entity == null)
23
{
24
return NotFound();
25
}
26
27
DoggoDto item = _mapper.Map(entity);
28
29
return Ok(item);
30
}
31
32
[HttpPost(Name = nameof(AddDoggo))]
33
public async Task> AddDoggo(
34
[FromBody] DoggoCreateDto createDto)
35
{
36
if (createDto == null)
37
[HttpGet]
[Route("{id:guid}", Name = nameof(GetSingleDoggo))]
public ActionResult GetSingleDoggo(Guid id)
{
DoggoEntity? entity = _repository.GetSingle(id);
if (entity == null)
{
return NotFound();
}
DoggoDto item = _mapper.Map(entity);
return Ok(item);
}
namespace DoggoApi.Controllers
1
{
2
[ApiController]
3
[Route("api/[controller]")]
4
[Authorize("access:api")]
5
public class DoggosController : ControllerBase
6
{
7
[HttpGet(Name = nameof(GetAllDoggos))]
8
[AllowAnonymous]
9
public ActionResult GetAllDoggos()
10
{
11
List doggos = _repository.GetAll().ToList();
12
13
return Ok(_mapper.Map(doggos));
14
}
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[HttpPost(Name = nameof(AddDoggo))]
33
public async Task> AddDoggo(
34
[FromBody] DoggoCreateDto createDto)
35
{
36
if (createDto == null)
37
[HttpPost(Name = nameof(AddDoggo))]
public async Task> AddDoggo(
[FromBody] DoggoCreateDto createDto)
{
if (createDto == null)
namespace DoggoApi.Controllers
1
{
2
[ApiController]
3
[Route("api/[controller]")]
4
[Authorize("access:api")]
5
public class DoggosController : ControllerBase
6
{
7
[HttpGet(Name = nameof(GetAllDoggos))]
8
[AllowAnonymous]
9
public ActionResult GetAllDoggos()
10
{
11
List doggos = _repository.GetAll().ToList();
12
13
return Ok(_mapper.Map(doggos));
14
}
15
16
[HttpGet]
17
[Route("{id:guid}", Name = nameof(GetSingleDoggo))]
18
public ActionResult GetSingleDoggo(Guid id)
19
{
20
DoggoEntity? entity = _repository.GetSingle(id);
21
22
if (entity == null)
23
{
24
return NotFound();
25
}
26
27
DoggoDto item = _mapper.Map(entity);
28
29
return Ok(item);
30
}
31
32
33
34
35
36
37
namespace DoggoApi.Controllers
1
{
2
[ApiController]
3
[Route("api/[controller]")]
4
[Authorize("access:api")]
5
public class DoggosController : ControllerBase
6
{
7
[HttpGet(Name = nameof(GetAllDoggos))]
8
[AllowAnonymous]
9
public ActionResult GetAllDoggos()
10
{
11
List doggos = _repository.GetAll().ToList();
12
13
return Ok(_mapper.Map(doggos));
14
}
15
16
[HttpGet]
17
[Route("{id:guid}", Name = nameof(GetSingleDoggo))]
18
public ActionResult GetSingleDoggo(Guid id)
19
{
20
DoggoEntity? entity = _repository.GetSingle(id);
21
22
if (entity == null)
23
{
24
return NotFound();
25
}
26
27
DoggoDto item = _mapper.Map(entity);
28
29
return Ok(item);
30
}
31
32
[HttpPost(Name = nameof(AddDoggo))]
33
public async Task> AddDoggo(
34
[FromBody] DoggoCreateDto createDto)
35
{
36
if (createDto == null)
37
namespace DoggoApi.Controllers
1
{
2
[ApiController]
3
[Route("api/[controller]")]
4
[Authorize("access:api")]
5
public class DoggosController : ControllerBase
6
{
7
[HttpGet(Name = nameof(GetAllDoggos))]
8
[AllowAnonymous]
9
public ActionResult GetAllDoggos()
10
{
11
List doggos = _repository.GetAll().ToList();
12
13
return Ok(_mapper.Map(doggos));
14
}
15
16
[HttpGet]
17
[Route("{id:guid}", Name = nameof(GetSingleDoggo))]
18
public ActionResult GetSingleDoggo(Guid id)
19
{
20
DoggoEntity? entity = _repository.GetSingle(id);
21
22
if (entity == null)
23
{
24
return NotFound();
25
}
26
27
DoggoDto item = _mapper.Map(entity);
28
29
return Ok(item);
30
}
31
32
[HttpPost(Name = nameof(AddDoggo))]
33
public async Task> AddDoggo(
34
[FromBody] DoggoCreateDto createDto)
35
{
36
if (createDto == null)
37
await _hubContext.Clients.All.SendAsync("DoggoRated", dto);
/ ... /
42
43
await _hubContext.Clients.All.SendAsync("DoggoAdded", dto);
44
45
return CreatedAtRoute(nameof(GetSingleDoggo),
46
new { id = newItem?.Id }, dto);
47
}
48
49
[HttpPut]
50
[Route("rate/{id:guid}", Name = nameof(RateDoggo))]
51
[AllowAnonymous]
52
public async Task> RateDoggo(
53
Guid id, [FromBody] DoggoRateDto rateDto)
54
{
55
if (rateDto == null)
56
{
57
return BadRequest();
58
}
59
60
DoggoEntity? existingEntity = _repository.GetSingle(id);
61
62
existingEntity.RatingCount = existingEntity.RatingCount + 1;
63
existingEntity.RatingSum = existingEntity.RatingSum + rateDto.Value;
64
65
_repository.Update(existingEntity);
66
67
if (!_repository.Save())
68
{
69
throw new Exception("Updating an item failed on save.");
70
}
71
72
DoggoDto dto = _mapper.Map(existingEntity);
73
74
75
76
return Ok(dto);
77
}
78
}
79