Skip to content

Commit

Permalink
Fix namespace typo and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
0GiS0 committed Nov 24, 2023
1 parent 9b8a5e9 commit e664b4f
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 206 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/devcontainers/dotnet:0-7.0
FROM mcr.microsoft.com/devcontainers/dotnet:8.0

# Install SQL Tools: SQLPackage and sqlcmd
COPY mssql/installSQLtools.sh installSQLtools.sh
Expand Down
7 changes: 5 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"password": "P@ssword",
"emptyPasswordInput": false,
"savePassword": false,
"profileName": "mssql-container"
"profileName": "mssql-container",
"trustServerCertificate": true
}
]
},
Expand All @@ -36,7 +37,9 @@
"humao.rest-client",
"hashicorp.terraform",
"ms-azuretools.vscode-docker",
"redhat.vscode-yaml"
"redhat.vscode-yaml",
"GitHub.copilot",
"GitHub.copilot-chat"
]
}
},
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net7.0/tour-of-heroes-api.dll",
"program": "${workspaceFolder}/bin/Debug/net8.0/tour-of-heroes-api.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
Expand Down
82 changes: 29 additions & 53 deletions Controllers/HeroController.cs
Original file line number Diff line number Diff line change
@@ -1,110 +1,86 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using tour_of_heroes_api.Models;
using tour_of_heroes_api.Modesl;
using System.Linq;

namespace tour_of_heroes_api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class HeroController : ControllerBase
{
private readonly HeroContext _context;

public HeroController(HeroContext context)
private IHeroRepository _heroRepository;
public HeroController(IHeroRepository heroRepository)
{
_context = context;
_heroRepository = heroRepository;
}

// GET: api/Hero
[HttpGet]
public async Task<ActionResult<IEnumerable<Hero>>> GetHeroes()
public ActionResult<IEnumerable<Hero>> GetHeroes()
{
// Just for demo purposes 🤓
var hash = MD5.Create();

return await _context.Heroes.ToListAsync();
var heroes = _heroRepository.GetAll();
return Ok(heroes);
}

// GET: api/Hero/5
[HttpGet("{id}")]
public async Task<ActionResult<Hero>> GetHero(int id)
[HttpGet("{id}")]
public ActionResult<Hero> GetHero(int id)
{
var hero = await _context.Heroes.FindAsync(id);
var hero = _heroRepository.GetById(id);

if (hero == null)
{
return NotFound();
}

return hero;
return Ok(hero);
}

// PUT: api/Hero/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutHero(int id, Hero hero)
public ActionResult PutHero(int id, Hero hero)
{
if (id != hero.Id)
{
return BadRequest();
}

_context.Entry(hero).State = EntityState.Modified;
var heroToUpdate = _heroRepository.GetById(id);

try
if (heroToUpdate == null)
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!HeroExists(id))
{
return NotFound();
}
else
{
throw;
}
return NotFound();
}

heroToUpdate.Name = hero.Name;
heroToUpdate.AlterEgo = hero.AlterEgo;
heroToUpdate.Description = hero.Description;

_heroRepository.Update(heroToUpdate);

return NoContent();

}

// POST: api/Hero
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Hero>> PostHero(Hero hero)
public ActionResult<Hero> PostHero(Hero hero)
{
_context.Heroes.Add(hero);
await _context.SaveChangesAsync();

return CreatedAtAction(nameof(GetHero), new { id = hero.Id }, hero);
_heroRepository.Add(hero);

return Ok(hero);

}

// DELETE: api/Hero/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteHero(int id)
public IActionResult DeleteHero(int id)
{
var hero = await _context.Heroes.FindAsync(id);
if (hero == null)
{
return NotFound();
}

_context.Heroes.Remove(hero);
await _context.SaveChangesAsync();
_heroRepository.Delete(id);

return NoContent();
}

private bool HeroExists(int id)
{
return _context.Heroes.Any(e => e.Id == id);
}
}
}
2 changes: 1 addition & 1 deletion Models/Hero.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace tour_of_heroes_api.Modesl
namespace tour_of_heroes_api.Models
{
public class Hero
{
Expand Down
1 change: 0 additions & 1 deletion Models/HeroContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using tour_of_heroes_api.Modesl;

namespace tour_of_heroes_api.Models
{
Expand Down
47 changes: 24 additions & 23 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using tour_of_heroes_api.Models;

namespace tour_of_heroes_api
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddScoped<IHeroRepository, HeroRepository>();
builder.Services.AddControllers();builder.Services.AddDbContext<HeroContext>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddSwaggerGen(c =>
{
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>();
});
}
}
c.SwaggerDoc("v1", new() { Title = "tour_of_heroes_api", Version = "v1" });
});

var app = builder.Build();

app.UseCors("CorsPolicy");

app.UseSwagger();
app.UseSwaggerUI();

app.MapControllers();

app.Run();

70 changes: 0 additions & 70 deletions Startup.cs

This file was deleted.

9 changes: 1 addition & 8 deletions appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,5 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://+:5000"
}
}
}
"AllowedHosts": "*"
}
10 changes: 2 additions & 8 deletions appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://+:5000"
}
}
}
"AllowedHosts": "*"

}
Loading

0 comments on commit e664b4f

Please sign in to comment.