Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
434 changes: 434 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

42 changes: 12 additions & 30 deletions Controllers/CommandsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,48 @@ public CommandsController(ICommanderRepo repository, IMapper mapper)
_repository = repository;
_mapper = mapper;
}

//GET api/commands
[HttpGet]
public ActionResult <IEnumerable<CommandReadDto>> GetAllCommmands()
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)
[HttpGet("{id}", Name = "GetCommandById")]
public ActionResult<CommandReadDto> GetCommandById(int id)
{
var commandItem = _repository.GetCommandById(id);
if(commandItem != null)
{
if (commandItem != null)
return Ok(_mapper.Map<CommandReadDto>(commandItem));
}
return NotFound();
}

//POST api/commands
[HttpPost]
public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto)
public ActionResult<CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto)
{
var commandModel = _mapper.Map<Command>(commandCreateDto);
_repository.CreateCommand(commandModel);
_repository.SaveChanges();

var commandReadDto = _mapper.Map<CommandReadDto>(commandModel);

return CreatedAtRoute(nameof(GetCommandById), new {Id = commandReadDto.Id}, commandReadDto);
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)
{
if (commandModelFromRepo == null)
return NotFound();
}
_mapper.Map(commandUpdateDto, commandModelFromRepo);

_repository.UpdateCommand(commandModelFromRepo);

_repository.SaveChanges();

return NoContent();
}

Expand All @@ -80,25 +72,18 @@ public ActionResult UpdateCommand(int id, CommandUpdateDto commandUpdateDto)
public ActionResult PartialCommandUpdate(int id, JsonPatchDocument<CommandUpdateDto> patchDoc)
{
var commandModelFromRepo = _repository.GetCommandById(id);
if(commandModelFromRepo == null)
{
if (commandModelFromRepo == null)
return NotFound();
}

var commandToPatch = _mapper.Map<CommandUpdateDto>(commandModelFromRepo);
patchDoc.ApplyTo(commandToPatch, ModelState);

if(!TryValidateModel(commandToPatch))
{
if (!TryValidateModel(commandToPatch))
return ValidationProblem(ModelState);
}

_mapper.Map(commandToPatch, commandModelFromRepo);

_repository.UpdateCommand(commandModelFromRepo);

_repository.SaveChanges();

return NoContent();
}

Expand All @@ -107,15 +92,12 @@ public ActionResult PartialCommandUpdate(int id, JsonPatchDocument<CommandUpdate
public ActionResult DeleteCommand(int id)
{
var commandModelFromRepo = _repository.GetCommandById(id);
if(commandModelFromRepo == null)
{
if (commandModelFromRepo == null)
return NotFound();
}
_repository.DeleteCommand(commandModelFromRepo);
_repository.SaveChanges();

return NoContent();
}

}
}
4 changes: 3 additions & 1 deletion Data/SqlCommanderRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void CreateCommand(Command cmd)
}

_context.Commands.Add(cmd);
SaveChanges();
}

public void DeleteCommand(Command cmd)
Expand All @@ -31,6 +32,7 @@ public void DeleteCommand(Command cmd)
throw new ArgumentNullException(nameof(cmd));
}
_context.Commands.Remove(cmd);
SaveChanges();
}

public IEnumerable<Command> GetAllCommands()
Expand All @@ -50,7 +52,7 @@ public bool SaveChanges()

public void UpdateCommand(Command cmd)
{
//Nothing
SaveChanges();
}
}
}
17 changes: 0 additions & 17 deletions Dtos/CommandCreateDto.cs

This file was deleted.

40 changes: 40 additions & 0 deletions Dtos/CommandDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.ComponentModel.DataAnnotations;

namespace Commander.Dtos
{
public class CommandCreateDto
{
[Required]
[MaxLength(250)]
public string HowTo { get; set; }

[Required]
public string Line { get; set; }

[Required]
public string Platform { get; set; }
}

public class CommandReadDto
{
public int Id { get; set; }

public string HowTo { get; set; }

public string Line { get; set; }

}

public class CommandUpdateDto
{
[Required]
[MaxLength(250)]
public string HowTo { get; set; }

[Required]
public string Line { get; set; }

[Required]
public string Platform { get; set; }
}
}
12 changes: 0 additions & 12 deletions Dtos/CommandReadDto.cs

This file was deleted.

17 changes: 0 additions & 17 deletions Dtos/CommandUpdateDto.cs

This file was deleted.

Binary file modified obj/Debug/netcoreapp3.1/Commander.assets.cache
Binary file not shown.