mirror of
https://github.com/farcasclaudiu/PracticeCalendar.git
synced 2026-06-22 09:01:18 +03:00
Add project files.
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using PracticeCalendar.API.Model;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
using PracticeCalendar.Domain.Entities.Specifications;
|
||||
|
||||
namespace PrcaticeCalendar.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class EventsController : ControllerBase
|
||||
{
|
||||
private readonly IRepository<PracticeEvent> eventsRepo;
|
||||
private readonly IMapper mapper;
|
||||
private readonly ILogger<EventsController> _logger;
|
||||
|
||||
public EventsController(IRepository<PracticeEvent> eventsRepo,
|
||||
IMapper mapper,
|
||||
ILogger<EventsController> logger)
|
||||
{
|
||||
this.eventsRepo = eventsRepo;
|
||||
this.mapper = mapper;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet(Name = "GetAll")]
|
||||
public async Task<ActionResult<List<EventModel>>> Get()
|
||||
{
|
||||
var spec = new PracticeEventsWithAttendees();
|
||||
var repoList = await eventsRepo.ListAsync(spec);
|
||||
var evList = repoList.Select(x=> {
|
||||
var model = mapper.Map<EventModel>(x);
|
||||
model.Attendees = x.Attendees.Select(m=>mapper.Map<AttendeeModel>(m)).ToArray();
|
||||
return model;
|
||||
})
|
||||
.ToList();
|
||||
return evList;
|
||||
}
|
||||
|
||||
[HttpPost(Name = "Create practice event")]
|
||||
public async Task<IActionResult> CreateEvent(EventModel eventModel)
|
||||
{
|
||||
var practiceEvent = new PracticeEvent(eventModel.Title, eventModel.Description);
|
||||
foreach (var att in eventModel.Attendees)
|
||||
{
|
||||
practiceEvent.AddAttendee(new Attendee(att.Name, att.EmailAddress));
|
||||
}
|
||||
var result = await eventsRepo.AddAsync(practiceEvent);
|
||||
await eventsRepo.SaveChangesAsync();
|
||||
return Ok(mapper.Map<EventModel>(result));
|
||||
}
|
||||
|
||||
[HttpPut(Name = "Update practice event")]
|
||||
public async Task<IActionResult> UpdateEvent(EventModel eventModel)
|
||||
{
|
||||
var practiceEvent = await eventsRepo.GetByIdAsync(eventModel.Id);
|
||||
if(practiceEvent == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
practiceEvent.UpdateTitleAndDescription(eventModel.Title, eventModel.Description);
|
||||
await eventsRepo.UpdateAsync(practiceEvent);
|
||||
await eventsRepo.SaveChangesAsync();
|
||||
return Ok(mapper.Map<EventModel>(practiceEvent));
|
||||
}
|
||||
|
||||
[HttpDelete(Name = "Delete practice event")]
|
||||
public async Task<IActionResult> DeleteEvent(int practiceEventId)
|
||||
{
|
||||
var org = await eventsRepo.GetByIdAsync(practiceEventId);
|
||||
await eventsRepo.DeleteAsync(org);
|
||||
await eventsRepo.SaveChangesAsync();
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("accept/{eventId}/{attendeeId}")]
|
||||
public async Task<IActionResult> AttendeeAcceptEvent(int eventId, int attendeeId)
|
||||
{
|
||||
var spec = new PracticeEventByIdWithAttendees(eventId);
|
||||
var practiceEvent = await eventsRepo.GetBySpecAsync(spec);
|
||||
if (practiceEvent == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
practiceEvent.AttendeeAcceptEvent(attendeeId);
|
||||
await eventsRepo.SaveChangesAsync();
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("decline/{eventId}/{attendeeId}")]
|
||||
public async Task<IActionResult> AttendeeDeclineEvent(int eventId, int attendeeId)
|
||||
{
|
||||
var spec = new PracticeEventByIdWithAttendees(eventId);
|
||||
var practiceEvent = await eventsRepo.GetBySpecAsync(spec);
|
||||
if (practiceEvent == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
practiceEvent.AttendeeDeclineEvent(attendeeId);
|
||||
await eventsRepo.SaveChangesAsync();
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
|
||||
WORKDIR /app
|
||||
EXPOSE 80
|
||||
EXPOSE 443
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
|
||||
WORKDIR /src
|
||||
COPY ["PrcaticeCalendar/PrcaticeCalendar.csproj", "PrcaticeCalendar/"]
|
||||
RUN dotnet restore "PrcaticeCalendar/PrcaticeCalendar.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/PrcaticeCalendar"
|
||||
RUN dotnet build "PrcaticeCalendar.csproj" -c Release -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
RUN dotnet publish "PrcaticeCalendar.csproj" -c Release -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "PrcaticeCalendar.dll"]
|
||||
@@ -0,0 +1,34 @@
|
||||
using AutoMapper;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
|
||||
namespace PracticeCalendar.API.Model
|
||||
{
|
||||
public class EventModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
|
||||
public DateTime StartTime { get; set; }
|
||||
public DateTime EndTime { get; set; }
|
||||
|
||||
public AttendeeModel[] Attendees { get; set; } = new AttendeeModel[0];
|
||||
}
|
||||
|
||||
public class AttendeeModel
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string EmailAddress { get; set; }
|
||||
public bool IsAttending { get; set; }
|
||||
}
|
||||
|
||||
public class EventModelProfile : Profile
|
||||
{
|
||||
public EventModelProfile()
|
||||
{
|
||||
CreateMap<PracticeEvent, EventModel>();
|
||||
CreateMap<EventModel, PracticeEvent>();
|
||||
CreateMap<Attendee, AttendeeModel>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>78351d12-8544-4ae0-877a-3522681401ca</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
|
||||
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.5.1" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PracticeCalendar.Infrastructure\PracticeCalendar.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,69 @@
|
||||
using Hellang.Middleware.ProblemDetails;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using PracticeCalendar.Infrastructure;
|
||||
using PracticeCalendar.Infrastructure.Persistence;
|
||||
using System;
|
||||
|
||||
namespace PracticeCalendar
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddProblemDetails();
|
||||
|
||||
builder.Services.AddAutoMapper(typeof(Program));
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
//inject infrastructure
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseProblemDetails();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
|
||||
// Seed Database
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
|
||||
try
|
||||
{
|
||||
var context = services.GetRequiredService<ApplicationDbContext>();
|
||||
//context.Database.Migrate();
|
||||
context.Database.EnsureCreated();
|
||||
//SeedData.Initialize(services);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var logger = services.GetRequiredService<ILogger<Program>>();
|
||||
logger.LogError(ex, "An error occurred seeding the DB. {exceptionMessage}", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"profiles": {
|
||||
"PracticeCalendar": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://localhost:7267;http://localhost:5267"
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Docker": {
|
||||
"commandName": "Docker",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
|
||||
"publishAllPorts": true,
|
||||
"useSSL": true
|
||||
}
|
||||
},
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:58588",
|
||||
"sslPort": 44388
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"UseInMemoryDatabase": true,
|
||||
"ConnectionStrings": {
|
||||
"SqliteConnection": "Data Source=practicecalendar.sqlite"
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user