Skip to content
Merged
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
16 changes: 9 additions & 7 deletions Commander.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.7">
<PackageReference Include="AutoMapper" Version="16.1.1" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="10.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="10.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.8" />
</ItemGroup>


Expand Down
29 changes: 17 additions & 12 deletions Controllers/CommandsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public ActionResult<IEnumerable<CommandReadDto>> GetAllCommands()

//GET api/commands/{id}
[HttpGet("{id}", Name = "GetCommandById")]
public ActionResult<CommandReadDto> GetCommandById(int Id)
public ActionResult<CommandReadDto> GetCommandById(int id)
{
var commandItem = _repository.GetCommandById(Id);
var commandItem = _repository.GetCommandById(id);
if (commandItem != null)
{
return Ok(_mapper.Map<CommandReadDto>(commandItem));
Expand All @@ -45,21 +45,21 @@ public ActionResult<CommandReadDto> GetCommandById(int Id)

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

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

return CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto);
return CreatedAtRoute(nameof(GetCommandById), new { id = commandReadDto.Id }, commandReadDto);
}


//PUT api/commands/{1}
[HttpPut("{id}")]
public ActionResult<CommandReadDto> UpdateComand(int id, CommandUpdateDto command)
public ActionResult<CommandReadDto> UpdateCommand(int id, CommandUpdateDto command)
{
var commandModelRepo = _repository.GetCommandById(id);
if (commandModelRepo == null)
Expand All @@ -78,21 +78,26 @@ public ActionResult<CommandReadDto> UpdateComand(int id, CommandUpdateDto comman
[HttpPatch("{id}")]
public ActionResult PartialCommandUpdate(int id, JsonPatchDocument<CommandUpdateDto> patchDoc)
{
if (patchDoc == null)
{
return BadRequest();
}

var commandModelRepo = _repository.GetCommandById(id);
if (commandModelRepo == null)
{
return NotFound();
}

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

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

_mapper.Map(commadToPath, commandModelRepo);
_mapper.Map(commandToPatch, commandModelRepo);
_repository.UpdateCommand(commandModelRepo);
_repository.SaveChanges();

Expand All @@ -101,9 +106,9 @@ public ActionResult PartialCommandUpdate(int id, JsonPatchDocument<CommandUpdate

//DELETE api/commands/{id}
[HttpDelete("{id}")]
public ActionResult DeleteCommand(int Id)
public ActionResult DeleteCommand(int id)
{
var commandItem = _repository.GetCommandById(Id);
var commandItem = _repository.GetCommandById(id);
if (commandItem == null)
{
return NotFound();
Expand All @@ -116,4 +121,4 @@ public ActionResult DeleteCommand(int Id)

}

}
}
5 changes: 3 additions & 2 deletions Data/CommanderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public CommanderContext(DbContextOptions<CommanderContext> options) : base(optio
{

}
public DbSet<Command> Commands { get; set; }

public DbSet<Command> Commands => Set<Command>();

}
}
}
4 changes: 2 additions & 2 deletions Data/ICommanderRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface ICommanderRepo
bool SaveChanges();

IEnumerable<Command> GetAllCommands();
Command GetCommandById(int id);
Command? GetCommandById(int id);

void CreateCommand(Command command);

Expand All @@ -19,4 +19,4 @@ public interface ICommanderRepo

}

}
}
7 changes: 4 additions & 3 deletions Data/SqlCommanderRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace Commander.Data
{
public class SqlCommanderRepo : ICommanderRepo
{
private CommanderContext _context;
private readonly CommanderContext _context;

public SqlCommanderRepo(CommanderContext context)
{
_context = context;
Expand All @@ -27,7 +28,7 @@ public IEnumerable<Command> GetAllCommands()
return _context.Commands.ToList();
}

public Command GetCommandById(int id)
public Command? GetCommandById(int id)
{
return _context.Commands.FirstOrDefault(w => w.Id == id);
}
Expand Down Expand Up @@ -55,4 +56,4 @@ public void DeleteCommand(Command command)
}
}

}
}
8 changes: 4 additions & 4 deletions Dtos/CommandCreateDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ public class CommandCreateDto
{
[Required]
[MaxLength(250)]
public string HowTo { get; set; }
public string HowTo { get; set; } = string.Empty;

[Required]
public string Line { get; set; }
public string Line { get; set; } = string.Empty;

[Required]
public string Platform { get; set; }
public string Platform { get; set; } = string.Empty;

}

}
}
10 changes: 5 additions & 5 deletions Dtos/CommandReadDto.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System.ComponentModel.DataAnnotations;

namespace Commander.Dtos
{
public class CommandReadDto
{
public int Id { get; set; }

public string HowTo { get; set; }
public string HowTo { get; set; } = string.Empty;

public string Line { get; set; } = string.Empty;

public string Line { get; set; }
public string Platform { get; set; } = string.Empty;

}

}
}
8 changes: 4 additions & 4 deletions Dtos/CommandUpdateDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ public class CommandUpdateDto
{
[Required]
[MaxLength(250)]
public string HowTo { get; set; }
public string HowTo { get; set; } = string.Empty;

[Required]
public string Line { get; set; }
public string Line { get; set; } = string.Empty;

[Required]
public string Platform { get; set; }
public string Platform { get; set; } = string.Empty;

}

}
}
8 changes: 4 additions & 4 deletions Models/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public class Command

[Required]
[MaxLength(250)]
public string HowTo { get; set; }
public string HowTo { get; set; } = string.Empty;

[Required]
public string Line { get; set; }
public string Line { get; set; } = string.Empty;

[Required]
public string Platform { get; set; }
public string Platform { get; set; } = string.Empty;

}

}
}
57 changes: 35 additions & 22 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Commander.Data;
using Commander.Profiles;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<CommanderContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("CommanderConnection")));

builder.Services
.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});

builder.Services.AddAutoMapper(_ => { }, typeof(CommandsProfile).Assembly);
builder.Services.AddScoped<ICommanderRepo, SqlCommanderRepo>();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

namespace Commander
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
public partial class Program;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Use a valid partial Program declaration

This migration leaves Program.cs with public partial class Program;, but C# class declarations still require a body, so the project will not compile as soon as this file is built. If the intent is to expose Commander.Program for tests or EF tooling, declare it with an empty body such as public partial class Program { }.

Useful? React with 👍 / 👎.

}
4 changes: 2 additions & 2 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"launchUrl": "api/commands",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Commander": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"launchUrl": "api/commands",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
Loading
Loading