From aa10135caa47bed272763eae0b27f0762a1d1a86 Mon Sep 17 00:00:00 2001 From: 1eG0ist Date: Thu, 5 Dec 2024 00:58:43 +0300 Subject: [PATCH] vidos-40-min-Web-Api-Added --- Presence.Api/Controllers/GroupController.cs | 35 ++++++++++++++++ .../Controllers/WeatherForecastController.cs | 33 +++++++++++++++ .../Extensions/ServiceCollectionsExtension.cs | 20 +++++++++ Presence.Api/Presence.Api.csproj | 22 ++++++++++ Presence.Api/Presence.Api.http | 6 +++ Presence.Api/Program.cs | 29 +++++++++++++ Presence.Api/Properties/launchSettings.json | 41 +++++++++++++++++++ Presence.Api/Response/GroupResponse.cs | 9 ++++ Presence.Api/Response/UserResponse.cs | 8 ++++ Presence.Api/WeatherForecast.cs | 13 ++++++ Presence.Api/appsettings.Development.json | 8 ++++ Presence.Api/appsettings.json | 9 ++++ data/Repository/SQLGroupRepository.cs | 3 +- domain/Entity/GroupEntity.cs | 4 +- domain/Entity/UserEntity.cs | 15 +++++++ domain/Service/GroupService.cs | 22 ++++++++++ domain/UseCase/IGroupUseCase.cs | 4 +- 17 files changed, 277 insertions(+), 4 deletions(-) create mode 100644 Presence.Api/Controllers/GroupController.cs create mode 100644 Presence.Api/Controllers/WeatherForecastController.cs create mode 100644 Presence.Api/Extensions/ServiceCollectionsExtension.cs create mode 100644 Presence.Api/Presence.Api.csproj create mode 100644 Presence.Api/Presence.Api.http create mode 100644 Presence.Api/Program.cs create mode 100644 Presence.Api/Properties/launchSettings.json create mode 100644 Presence.Api/Response/GroupResponse.cs create mode 100644 Presence.Api/Response/UserResponse.cs create mode 100644 Presence.Api/WeatherForecast.cs create mode 100644 Presence.Api/appsettings.Development.json create mode 100644 Presence.Api/appsettings.json create mode 100644 domain/Entity/UserEntity.cs diff --git a/Presence.Api/Controllers/GroupController.cs b/Presence.Api/Controllers/GroupController.cs new file mode 100644 index 0000000..7d48a98 --- /dev/null +++ b/Presence.Api/Controllers/GroupController.cs @@ -0,0 +1,35 @@ +using domain.Service; +using domain.UseCase; +using Microsoft.AspNetCore.Mvc; +using Presence.Api.Response; + +namespace Presence.Api.Controllers +{ + [ApiController] + [Route("[controller]/api")] + public class GroupController: ControllerBase + { + private readonly IGroupUseCase _groupService; + + public GroupController(IGroupUseCase groupService) + { + _groupService = groupService; + } + + [HttpGet] + public ActionResult GetAllGroups() + { + var result = _groupService + .GetGroupsWithStudents() + .Select(group => new GroupResponse { + Id = group.Id, + Name = group.Name, + Users = group.Users.Select(user => new UserResponse { + Guid = user.Guid, + Name = user.Name + }).Take(10).ToList(), + }).ToList(); + return Ok(new GroupResponse()); + } + } +} diff --git a/Presence.Api/Controllers/WeatherForecastController.cs b/Presence.Api/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..4ad6679 --- /dev/null +++ b/Presence.Api/Controllers/WeatherForecastController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Presence.Api.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 _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/Presence.Api/Extensions/ServiceCollectionsExtension.cs b/Presence.Api/Extensions/ServiceCollectionsExtension.cs new file mode 100644 index 0000000..4321298 --- /dev/null +++ b/Presence.Api/Extensions/ServiceCollectionsExtension.cs @@ -0,0 +1,20 @@ +using data; +using data.Repository; +using domain.Service; +using domain.UseCase; +using Presence.Api.Controllers; + +namespace Presence.Api.Extensions +{ + public static class ServiceCollectionsExtension + { + public static void AddCommomService(this IServiceCollection services) + { + services + .AddDbContext() + .AddScoped() + .AddScoped() + .AddScoped(); + } + } +} diff --git a/Presence.Api/Presence.Api.csproj b/Presence.Api/Presence.Api.csproj new file mode 100644 index 0000000..3c0d11b --- /dev/null +++ b/Presence.Api/Presence.Api.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + + + diff --git a/Presence.Api/Presence.Api.http b/Presence.Api/Presence.Api.http new file mode 100644 index 0000000..68c2819 --- /dev/null +++ b/Presence.Api/Presence.Api.http @@ -0,0 +1,6 @@ +@Presence.Api_HostAddress = http://localhost:5028 + +GET {{Presence.Api_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Presence.Api/Program.cs b/Presence.Api/Program.cs new file mode 100644 index 0000000..d640dd2 --- /dev/null +++ b/Presence.Api/Program.cs @@ -0,0 +1,29 @@ +using Presence.Api.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +builder.Services.AddCommomService(); + +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(); diff --git a/Presence.Api/Properties/launchSettings.json b/Presence.Api/Properties/launchSettings.json new file mode 100644 index 0000000..10c6513 --- /dev/null +++ b/Presence.Api/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:32613", + "sslPort": 44302 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5028", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7098;http://localhost:5028", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Presence.Api/Response/GroupResponse.cs b/Presence.Api/Response/GroupResponse.cs new file mode 100644 index 0000000..c252f0c --- /dev/null +++ b/Presence.Api/Response/GroupResponse.cs @@ -0,0 +1,9 @@ +namespace Presence.Api.Response +{ + public class GroupResponse + { + public int Id { get; set; } + public string Name { get; set; } + public IEnumerable? Users { get; set; } = null; + } +} diff --git a/Presence.Api/Response/UserResponse.cs b/Presence.Api/Response/UserResponse.cs new file mode 100644 index 0000000..c4f1df2 --- /dev/null +++ b/Presence.Api/Response/UserResponse.cs @@ -0,0 +1,8 @@ +namespace Presence.Api.Response +{ + public class UserResponse + { + public Guid Guid { get; set; } + public string Name { get; set; } + } +} diff --git a/Presence.Api/WeatherForecast.cs b/Presence.Api/WeatherForecast.cs new file mode 100644 index 0000000..86ff197 --- /dev/null +++ b/Presence.Api/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace Presence.Api +{ + public class WeatherForecast + { + public DateOnly Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } + } +} diff --git a/Presence.Api/appsettings.Development.json b/Presence.Api/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Presence.Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Presence.Api/appsettings.json b/Presence.Api/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Presence.Api/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/data/Repository/SQLGroupRepository.cs b/data/Repository/SQLGroupRepository.cs index 5b55eef..2c34b3b 100644 --- a/data/Repository/SQLGroupRepository.cs +++ b/data/Repository/SQLGroupRepository.cs @@ -1,4 +1,5 @@ using data.DAO; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -30,7 +31,7 @@ namespace data.Repository { try { - return dbContext.groups.ToList(); + return dbContext.groups.Include(group => group.Users).ToList(); } catch (Exception ex) { return new List(); diff --git a/domain/Entity/GroupEntity.cs b/domain/Entity/GroupEntity.cs index 786c70b..2bdbe91 100644 --- a/domain/Entity/GroupEntity.cs +++ b/domain/Entity/GroupEntity.cs @@ -6,11 +6,11 @@ using System.Threading.Tasks; namespace domain.Entity { - internal class GroupEntity + public class GroupEntity { public int Id { get; set; } public string Name { get; set; } - public string Description { get; set; } + public IEnumerable Users { get; set; } = null; } } diff --git a/domain/Entity/UserEntity.cs b/domain/Entity/UserEntity.cs new file mode 100644 index 0000000..bf5430d --- /dev/null +++ b/domain/Entity/UserEntity.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace domain.Entity +{ + public class UserEntity + { + public Guid Guid { get; set; } + public string Name { get; set; } + public GroupEntity Group { get; set; } + } +} diff --git a/domain/Service/GroupService.cs b/domain/Service/GroupService.cs index c6b0600..21368cf 100644 --- a/domain/Service/GroupService.cs +++ b/domain/Service/GroupService.cs @@ -1,5 +1,6 @@ using data.DAO; using data.Repository; +using domain.Entity; using domain.Request; using domain.UseCase; using System; @@ -30,5 +31,26 @@ namespace domain.Service .ToList(); _groupRepository.addGroupWithStudents(groupDAO, users); } + + public IEnumerable GetGroupsWithStudents() + { + return _groupRepository.getAllGroup().Select( + group => new GroupEntity() + { + Id = group.Id, + Name = group.Name, + Users = group.Users.Select( + user => new UserEntity + { + Guid = user.Guid, + Name = user.Name, + Group = new GroupEntity + { + Id = group.Id, + Name = group.Name, + } + }).ToList() + }).ToList(); + } } } diff --git a/domain/UseCase/IGroupUseCase.cs b/domain/UseCase/IGroupUseCase.cs index e4811ba..7e027d8 100644 --- a/domain/UseCase/IGroupUseCase.cs +++ b/domain/UseCase/IGroupUseCase.cs @@ -1,4 +1,5 @@ -using domain.Request; +using domain.Entity; +using domain.Request; using System; using System.Collections.Generic; using System.Linq; @@ -9,6 +10,7 @@ namespace domain.UseCase { public interface IGroupUseCase { + public IEnumerable GetGroupsWithStudents(); public void AddGroup(AddGroupRequest addGroupRequest); public void AddGroupWithStudents(AddGroupWithStudentsRequest addGroupWithStudents); }