-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathCommandsController.cs
More file actions
103 lines (83 loc) · 3.25 KB
/
CommandsController.cs
File metadata and controls
103 lines (83 loc) · 3.25 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Collections.Generic;
using AutoMapper;
using Commander.Data;
using Commander.Dtos;
using Commander.Models;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Mvc;
namespace Commander.Controllers
{
[Route("api/commands")]
[ApiController]
public class CommandsController : ControllerBase
{
private readonly ICommanderRepo _repository;
private readonly IMapper _mapper;
public CommandsController(ICommanderRepo repository, IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
//GET api/commands
[HttpGet]
public ActionResult<IEnumerable<CommandReadDto>> GetAllCommmands()
{
var commandItems = _repository.GetAllCommands();
return Ok(_mapper.Map<IEnumerable<CommandReadDto>>(commandItems));
}
//GET api/commands/{id}
[HttpGet("{id}", Name = "GetCommandById")]
public ActionResult<CommandReadDto> GetCommandById(int id)
{
var commandItem = _repository.GetCommandById(id);
if (commandItem != null)
return Ok(_mapper.Map<CommandReadDto>(commandItem));
return NotFound();
}
//POST api/commands
[HttpPost]
public ActionResult<CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto)
{
var commandModel = _mapper.Map<Command>(commandCreateDto);
_repository.CreateCommand(commandModel);
var commandReadDto = _mapper.Map<CommandReadDto>(commandModel);
return CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto);
}
//PUT api/commands/{id}
[HttpPut("{id}")]
public ActionResult UpdateCommand(int id, CommandUpdateDto commandUpdateDto)
{
var commandModelFromRepo = _repository.GetCommandById(id);
if (commandModelFromRepo == null)
return NotFound();
_mapper.Map(commandUpdateDto, commandModelFromRepo);
_repository.UpdateCommand(commandModelFromRepo);
return NoContent();
}
//PATCH api/commands/{id}
[HttpPatch("{id}")]
public ActionResult PartialCommandUpdate(int id, JsonPatchDocument<CommandUpdateDto> patchDoc)
{
var commandModelFromRepo = _repository.GetCommandById(id);
if (commandModelFromRepo == null)
return NotFound();
var commandToPatch = _mapper.Map<CommandUpdateDto>(commandModelFromRepo);
patchDoc.ApplyTo(commandToPatch, ModelState);
if (!TryValidateModel(commandToPatch))
return ValidationProblem(ModelState);
_mapper.Map(commandToPatch, commandModelFromRepo);
_repository.UpdateCommand(commandModelFromRepo);
return NoContent();
}
//DELETE api/commands/{id}
[HttpDelete("{id}")]
public ActionResult DeleteCommand(int id)
{
var commandModelFromRepo = _repository.GetCommandById(id);
if (commandModelFromRepo == null)
return NotFound();
_repository.DeleteCommand(commandModelFromRepo);
return NoContent();
}
}
}