-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
88 lines (63 loc) · 2.43 KB
/
Program.cs
File metadata and controls
88 lines (63 loc) · 2.43 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
using AutoMapper;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using SixMinAPI.Data;
using SixMinAPI.Dtos;
using SixMinAPI.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var sqlConBuilder = new SqlConnectionStringBuilder();
sqlConBuilder.ConnectionString = builder.Configuration.GetConnectionString("SQLDbConnection");
sqlConBuilder.UserID = builder.Configuration["UserID"];
sqlConBuilder.Password = builder.Configuration["Password"];
builder.Services.AddDbContext<AppDbContext>(opt => opt.UseSqlServer(sqlConBuilder.ConnectionString));
builder.Services.AddScoped<ICommandRepo, CommandRepo>();
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("api/v1/commands", async (ICommandRepo repo, IMapper mapper) => {
var commands = await repo.GetAllCommands();
return Results.Ok(mapper.Map<IEnumerable<CommandReadDto>>(commands));
});
app.MapGet("api/v1/commands/{id}", async (ICommandRepo repo, IMapper mapper, int id) => {
var command = await repo.GetCommandById(id);
if (command != null)
{
return Results.Ok(mapper.Map<CommandReadDto>(command));
}
return Results.NotFound();
});
app.MapPost("api/v1/commands", async (ICommandRepo repo, IMapper mapper, CommandCreateDto cmdCreateDto) => {
var commandModel = mapper.Map<Command>(cmdCreateDto);
await repo.CreateCommand(commandModel);
await repo.SaveChanges();
var cmdReadDto = mapper.Map<CommandReadDto>(commandModel);
return Results.Created($"api/v1/commands/{cmdReadDto.Id}", cmdReadDto);
});
app.MapPut("api/v1/commands/{id}", async (ICommandRepo repo, IMapper mapper, int id, CommandUpdateDto cmdUpdateDto) => {
var command = await repo.GetCommandById(id);
if (command == null)
{
return Results.NotFound();
}
mapper.Map(cmdUpdateDto, command);
await repo.SaveChanges();
return Results.NoContent();
});
app.MapDelete("api/v1/commands/{id}", async (ICommandRepo repo, IMapper mapper, int id) => {
var command = await repo.GetCommandById(id);
if (command == null)
{
return Results.NotFound();
}
repo.DeleteCommand(command);
await repo.SaveChanges();
return Results.NoContent();
});
app.Run();