-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mrvoi
authored and
mrvoi
committed
May 12, 2022
1 parent
ffc37e8
commit 871e441
Showing
14 changed files
with
416 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.2.32505.173 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SuperHeroAPI", "SuperHeroAPI\SuperHeroAPI.csproj", "{BC38AEA6-59BB-4CA3-ACA9-6B820A447964}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{BC38AEA6-59BB-4CA3-ACA9-6B820A447964}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{BC38AEA6-59BB-4CA3-ACA9-6B820A447964}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{BC38AEA6-59BB-4CA3-ACA9-6B820A447964}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{BC38AEA6-59BB-4CA3-ACA9-6B820A447964}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {0B54823A-7043-4859-8EC0-998B1F4AC010} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace SuperHeroAPI.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class SuperHeroController : ControllerBase | ||
{ | ||
private readonly DataContext _context; | ||
|
||
public SuperHeroController(DataContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
[HttpGet] | ||
public async Task<ActionResult<List<SuperHero>>> Get() | ||
{ | ||
return Ok(await _context.SuperHeros.ToListAsync()); | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public async Task<ActionResult<List<SuperHero>>> Get(int id) | ||
{ | ||
var hero = await _context.SuperHeros.FindAsync(id); | ||
if (hero == null) | ||
{ | ||
return BadRequest("Hero not found."); | ||
} | ||
return Ok(hero); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<ActionResult<List<SuperHero>>> AddHero(SuperHero hero) | ||
{ | ||
_context.SuperHeros.Add(hero); | ||
await _context.SaveChangesAsync(); | ||
return Ok(await _context.SuperHeros.ToListAsync()); | ||
} | ||
|
||
[HttpPut] | ||
public async Task<ActionResult<List<SuperHero>>> UpdateHero(SuperHero request) | ||
{ | ||
var dbhero = await _context.SuperHeros.FindAsync(request.Id); | ||
if (dbhero == null) | ||
{ | ||
return BadRequest("Hero not found."); | ||
} | ||
|
||
dbhero.Name = request.Name; | ||
dbhero.FirstName = request.FirstName; | ||
dbhero.LastName = request.LastName; | ||
dbhero.Place = request.Place; | ||
|
||
await _context.SaveChangesAsync(); | ||
|
||
return Ok(await _context.SuperHeros.ToListAsync()); | ||
} | ||
|
||
[HttpDelete] | ||
public async Task<ActionResult<List<SuperHero>>> Delete(int id) | ||
{ | ||
var dbhero = await _context.SuperHeros.FindAsync(id); | ||
if (dbhero == null) | ||
{ | ||
return BadRequest("Hero not found."); | ||
} | ||
|
||
_context.SuperHeros.Remove(dbhero); | ||
await _context.SaveChangesAsync(); | ||
return Ok(await _context.SuperHeros.ToListAsync()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace SuperHeroAPI.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[HttpGet(Name = "GetWeatherForecast")] | ||
public IEnumerable<WeatherForecast> Get() | ||
{ | ||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateTime.Now.AddDays(index), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}) | ||
.ToArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace SuperHeroAPI.Data | ||
{ | ||
public class DataContext :DbContext | ||
{ | ||
public DataContext(DbContextOptions<DataContext> options) : base(options) { } | ||
|
||
public DbSet<SuperHero> SuperHeros { get; set; } | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
SuperHeroAPI/Migrations/20220511235547_CreateInitial.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
||
namespace SuperHeroAPI.Migrations | ||
{ | ||
public partial class CreateInitial : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "SuperHeros", | ||
columns: table => new | ||
{ | ||
Id = table.Column<int>(type: "int", nullable: false) | ||
.Annotation("SqlServer:Identity", "1, 1"), | ||
Name = table.Column<string>(type: "nvarchar(max)", nullable: false), | ||
FirstName = table.Column<string>(type: "nvarchar(max)", nullable: false), | ||
LastName = table.Column<string>(type: "nvarchar(max)", nullable: false), | ||
Place = table.Column<string>(type: "nvarchar(max)", nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_SuperHeros", x => x.Id); | ||
}); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "SuperHeros"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// <auto-generated /> | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using SuperHeroAPI.Data; | ||
|
||
#nullable disable | ||
|
||
namespace SuperHeroAPI.Migrations | ||
{ | ||
[DbContext(typeof(DataContext))] | ||
partial class DataContextModelSnapshot : ModelSnapshot | ||
{ | ||
protected override void BuildModel(ModelBuilder modelBuilder) | ||
{ | ||
#pragma warning disable 612, 618 | ||
modelBuilder | ||
.HasAnnotation("ProductVersion", "6.0.5") | ||
.HasAnnotation("Relational:MaxIdentifierLength", 128); | ||
|
||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); | ||
|
||
modelBuilder.Entity("SuperHeroAPI.SuperHero", b => | ||
{ | ||
b.Property<int>("Id") | ||
.ValueGeneratedOnAdd() | ||
.HasColumnType("int"); | ||
|
||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | ||
|
||
b.Property<string>("FirstName") | ||
.IsRequired() | ||
.HasColumnType("nvarchar(max)"); | ||
|
||
b.Property<string>("LastName") | ||
.IsRequired() | ||
.HasColumnType("nvarchar(max)"); | ||
|
||
b.Property<string>("Name") | ||
.IsRequired() | ||
.HasColumnType("nvarchar(max)"); | ||
|
||
b.Property<string>("Place") | ||
.IsRequired() | ||
.HasColumnType("nvarchar(max)"); | ||
|
||
b.HasKey("Id"); | ||
|
||
b.ToTable("SuperHeros"); | ||
}); | ||
#pragma warning restore 612, 618 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
global using SuperHeroAPI.Data; | ||
global using Microsoft.EntityFrameworkCore; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
builder.Services.AddDbContext<DataContext>(options => | ||
{ | ||
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")); | ||
}); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:12843", | ||
"sslPort": 44318 | ||
} | ||
}, | ||
"profiles": { | ||
"SuperHeroAPI": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7054;http://localhost:5054", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace SuperHeroAPI | ||
{ | ||
public class SuperHero | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } = string.Empty; | ||
public string FirstName { get; set; } = string.Empty; | ||
public string LastName { get; set; } = string.Empty; | ||
public string Place { get; set; } = string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.5"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.5" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.