-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathProgram.cs
More file actions
145 lines (131 loc) · 4.91 KB
/
Copy pathProgram.cs
File metadata and controls
145 lines (131 loc) · 4.91 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Ardalis.Sample.Domain;
using Ardalis.Sample.Domain.Specs;
using Ardalis.Specification;
using Ardalis.Specification.EntityFrameworkCore;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DbConnection");
builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
builder.Services.AddScoped(typeof(IReadRepository<>), typeof(ReadRepository<>));
builder.Services.AddAutoMapper(typeof(MappingProfile).Assembly);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.MapGet("/customers", async (IReadRepository<Customer> repo, IMapper mapper, CancellationToken cancellationToken) =>
{
var spec = new CustomerSpec();
var customers = await repo.ListAsync(spec, cancellationToken);
var customersDto = mapper.Map<List<CustomerDto>>(customers);
return Results.Ok(customersDto);
});
app.MapGet("/customers/{id}", async (IReadRepository<Customer> repo, IMapper mapper, int id, CancellationToken cancellationToken) =>
{
var spec = new CustomerByIdSpec(id);
var customer = await repo.FirstOrDefaultAsync(spec, cancellationToken);
if (customer is null) return Results.NotFound();
var customerDto = mapper.Map<CustomerDto>(customer);
return Results.Ok(customerDto);
});
app.MapPost("/customers", async (IRepository<Customer> repo, IMapper mapper, CustomerCreateDto customerCreateDto, CancellationToken cancellationToken) =>
{
var customer = new Customer
{
Name = customerCreateDto.Name,
Age = customerCreateDto.Age,
Addresses = customerCreateDto.Addresses.Select(a => new Address { Street = a.Street }).ToList()
};
await repo.AddAsync(customer, cancellationToken);
var customerDto = mapper.Map<CustomerDto>(customer);
return Results.Ok(customerDto);
});
app.MapPut("/customers/{id}", async (IRepository<Customer> repo, IMapper mapper, int id, CustomerUpdateDto customerUpdate, CancellationToken cancellationToken) =>
{
var spec = new CustomerByIdSpec(id);
var customer = await repo.FirstOrDefaultAsync(spec, cancellationToken);
if (customer is null) return Results.NotFound();
customer.Name = customerUpdate.Name;
customer.Age = customerUpdate.Age;
await repo.UpdateAsync(customer, cancellationToken);
var customerDto = mapper.Map<CustomerDto>(customer);
return Results.Ok(customerDto);
});
await app.InitializeDbAsync();
app.Run();
public record AddressDto(int Id, string Street, int CustomerId);
public record AddressCreateDto(string Street);
public record CustomerDto(int Id, string Name, int Age, List<AddressDto> Addresses);
public record CustomerCreateDto(string Name, int Age, List<AddressCreateDto> Addresses);
public record CustomerUpdateDto(string Name, int Age);
public class AppDbContext : DbContext
{
public DbSet<Customer> Customers => Set<Customer>();
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
}
public interface IRepository<T> : IRepositoryBase<T> where T : class, IAggregateRoot
{
}
public interface IReadRepository<T> : IReadRepositoryBase<T> where T : class
{
}
public class Repository<T> : RepositoryBase<T>, IRepository<T> where T : class, IAggregateRoot
{
public Repository(AppDbContext dbContext) : base(dbContext)
{
}
}
public class ReadRepository<T> : RepositoryBase<T>, IReadRepository<T> where T : class
{
public ReadRepository(AppDbContext dbContext) : base(dbContext)
{
}
}
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Address, AddressDto>();
CreateMap<Customer, CustomerDto>();
}
}
public static class WebApplicationExtensions
{
public static async Task InitializeDbAsync(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await dbContext.Database.EnsureDeletedAsync();
await dbContext.Database.EnsureCreatedAsync();
var customers = new List<Customer>()
{
new()
{
Name = "Customer1",
Age = 20,
Addresses = new()
{
new() { Street = "Street1_1" },
new() { Street = "Street1_2" }
}
},
new()
{
Name = "Customer2",
Age = 30,
Addresses = new()
{
new() { Street = "Street2_1" },
new() { Street = "Street3_2" }
}
}
};
dbContext.Customers.AddRange(customers);
await dbContext.SaveChangesAsync();
}
}