vidos-40-min-Web-Api-Added
This commit is contained in:
parent
189d95daa6
commit
aa10135caa
35
Presence.Api/Controllers/GroupController.cs
Normal file
35
Presence.Api/Controllers/GroupController.cs
Normal file
@ -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<GroupResponse> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
Presence.Api/Controllers/WeatherForecastController.cs
Normal file
33
Presence.Api/Controllers/WeatherForecastController.cs
Normal file
@ -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<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 = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||||
|
TemperatureC = Random.Shared.Next(-20, 55),
|
||||||
|
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||||
|
})
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
Presence.Api/Extensions/ServiceCollectionsExtension.cs
Normal file
20
Presence.Api/Extensions/ServiceCollectionsExtension.cs
Normal file
@ -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<RemoteDatabaseContext>()
|
||||||
|
.AddScoped<IGroupRepository, SQLGroupRepository>()
|
||||||
|
.AddScoped<IGroupUseCase, GroupService>()
|
||||||
|
.AddScoped<GroupController>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
Presence.Api/Presence.Api.csproj
Normal file
22
Presence.Api/Presence.Api.csproj
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\domain\domain.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Request\" />
|
||||||
|
<Folder Include="ServiceCollectionsExtension\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
6
Presence.Api/Presence.Api.http
Normal file
6
Presence.Api/Presence.Api.http
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
@Presence.Api_HostAddress = http://localhost:5028
|
||||||
|
|
||||||
|
GET {{Presence.Api_HostAddress}}/weatherforecast/
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
29
Presence.Api/Program.cs
Normal file
29
Presence.Api/Program.cs
Normal file
@ -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();
|
41
Presence.Api/Properties/launchSettings.json
Normal file
41
Presence.Api/Properties/launchSettings.json
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
Presence.Api/Response/GroupResponse.cs
Normal file
9
Presence.Api/Response/GroupResponse.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
namespace Presence.Api.Response
|
||||||
|
{
|
||||||
|
public class GroupResponse
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public IEnumerable<UserResponse>? Users { get; set; } = null;
|
||||||
|
}
|
||||||
|
}
|
8
Presence.Api/Response/UserResponse.cs
Normal file
8
Presence.Api/Response/UserResponse.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace Presence.Api.Response
|
||||||
|
{
|
||||||
|
public class UserResponse
|
||||||
|
{
|
||||||
|
public Guid Guid { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
}
|
13
Presence.Api/WeatherForecast.cs
Normal file
13
Presence.Api/WeatherForecast.cs
Normal file
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
8
Presence.Api/appsettings.Development.json
Normal file
8
Presence.Api/appsettings.Development.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
Presence.Api/appsettings.json
Normal file
9
Presence.Api/appsettings.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using data.DAO;
|
using data.DAO;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -30,7 +31,7 @@ namespace data.Repository
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return dbContext.groups.ToList();
|
return dbContext.groups.Include(group => group.Users).ToList();
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
return new List<GroupDAO>();
|
return new List<GroupDAO>();
|
||||||
|
@ -6,11 +6,11 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace domain.Entity
|
namespace domain.Entity
|
||||||
{
|
{
|
||||||
internal class GroupEntity
|
public class GroupEntity
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Description { get; set; }
|
public IEnumerable<UserEntity> Users { get; set; } = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
domain/Entity/UserEntity.cs
Normal file
15
domain/Entity/UserEntity.cs
Normal file
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using data.DAO;
|
using data.DAO;
|
||||||
using data.Repository;
|
using data.Repository;
|
||||||
|
using domain.Entity;
|
||||||
using domain.Request;
|
using domain.Request;
|
||||||
using domain.UseCase;
|
using domain.UseCase;
|
||||||
using System;
|
using System;
|
||||||
@ -30,5 +31,26 @@ namespace domain.Service
|
|||||||
.ToList();
|
.ToList();
|
||||||
_groupRepository.addGroupWithStudents(groupDAO, users);
|
_groupRepository.addGroupWithStudents(groupDAO, users);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerable<GroupEntity> 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using domain.Request;
|
using domain.Entity;
|
||||||
|
using domain.Request;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,6 +10,7 @@ namespace domain.UseCase
|
|||||||
{
|
{
|
||||||
public interface IGroupUseCase
|
public interface IGroupUseCase
|
||||||
{
|
{
|
||||||
|
public IEnumerable<GroupEntity> GetGroupsWithStudents();
|
||||||
public void AddGroup(AddGroupRequest addGroupRequest);
|
public void AddGroup(AddGroupRequest addGroupRequest);
|
||||||
public void AddGroupWithStudents(AddGroupWithStudentsRequest addGroupWithStudents);
|
public void AddGroupWithStudents(AddGroupWithStudentsRequest addGroupWithStudents);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user