Skip to content

Commit

Permalink
Update Loading servie, Create, List, Edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Dębowski committed Dec 2, 2024
1 parent 47c957e commit e570a1b
Show file tree
Hide file tree
Showing 35 changed files with 538 additions and 85 deletions.
14 changes: 14 additions & 0 deletions src/hiPower.Abstracts/IServerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace hiPower.Abstracts;

public interface IServerService
{
Task<ErrorOr<IEnumerable<Server>>> GetAllAsync ();
Task<ErrorOr<IEnumerable<Server>>> GetAllAsync (string dataCenterId);
Task<ErrorOr<Server>> GetAsync (string id);

Task<ErrorOr<Server>> CreateAsync(Server server);

Task<ErrorOr<Server>> UpdateAsync(string id, Server server);

Task<ErrorOr<bool>> DeleteAsync (string id);
}
2 changes: 1 addition & 1 deletion src/hiPower.Abstracts/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace hiPower.Abstracts;

public interface IUnitOfWork : IDisposable
{
IGenericRepository<ServerLocation> LocationRepository { get; }
IGenericRepository<ServerLocation> DataCenterRepository { get; }
IGenericRepository<Entity.Server> ServerRepository { get; }
Task SaveAsync ();
}
11 changes: 11 additions & 0 deletions src/hiPower.Common.Type/Errors/ServerServiceErrors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using ErrorOr;

namespace hiPower.Common.Type.Errors;

public static class ServerServiceErrors
{
public static Error UpdateError => Error.Failure (
code: "UpdateError",
description: "Unable to update"
);
}
18 changes: 10 additions & 8 deletions src/hiPower.Core/DataCenterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,38 @@
namespace hiPower.Core;

public class DataCenterService(IUnitOfWork unit,
IMapper mapper) : IDataCenterService
IMapper mapper) : IDataCenterService
{
public async Task<ErrorOr<DataCenter>> CreateAsync (DataCenter location)
{
var result = await unit.LocationRepository.CreateAsync(location.Adapt<ServerLocation>());
var result = await unit.DataCenterRepository.CreateAsync(location.Adapt<ServerLocation>());
await unit.SaveAsync ();
return mapper.Map<DataCenter> (result);
}

public async Task<ErrorOr<bool>> DeleteAsync (string id)
{
await unit.LocationRepository.DeleteAsync(id);
await unit.DataCenterRepository.DeleteAsync(id);
return true;
}

public async Task<ErrorOr<DataCenter>> GetAsync (string id)
{
var result = await unit.LocationRepository.GetAsync(x => x.Id.Equals(id.ToUpperInvariant()), null);
var result = await unit.DataCenterRepository.GetAsync(x => x.Id.Equals(id.ToUpperInvariant()), null);
return result.Adapt<DataCenter> ();
}

public async Task<ErrorOr<IEnumerable<DataCenter>>> GetAsync ()
{
var result = await unit.LocationRepository.GetAll(null, null,null);
var result = await unit.DataCenterRepository.GetAll(null, null,null);
return result.Adapt<IEnumerable<DataCenter>> ().ToErrorOr ();
}

public Task<ErrorOr<IEnumerable<Dto.Manager.Server>>> GetServers (string id)
public async Task<ErrorOr<IEnumerable<Dto.Manager.Server>>> GetServers (string id)
{
throw new NotImplementedException ();
var result = await unit.ServerRepository.GetAll(x => x.LocationId.Equals(id.ToUpper()), null, null);
return result.Adapt<IEnumerable<Dto.Manager.Server>> ()
.ToErrorOr();
}

public async Task<ErrorOr<DataCenter>> UpdateAsync (string id, DataCenter dataCenter)
Expand All @@ -42,7 +44,7 @@ public async Task<ErrorOr<DataCenter>> UpdateAsync (string id, DataCenter dataCe
{
return Error.NotFound (dataCenter.Id);
}
var result = unit.LocationRepository
var result = unit.DataCenterRepository
.Update(dataCenter.Adapt<ServerLocation>())
.ToErrorOr();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class CoreServicesExtensions
public static IServiceCollection ConfigureCoreServices(this IServiceCollection services)
{
services.AddScoped<IDataCenterService, DataCenterService> ();
services.AddScoped<IServerService, ServerService> ();

services.ConfigureMapster ();

Expand Down
20 changes: 11 additions & 9 deletions src/hiPower.Core/Mappings/LocationMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ internal class LocationMapping : IRegister
{
public void Register (TypeAdapterConfig config)
{
config.NewConfig<DataCenter, ServerLocation>()
.Map(dest => dest.Id, src => src.Id)
.Map(dest => dest.Name, src => src.Name)
.Map(dest => dest.Address, src => src.Address)
.Map(dest => dest.City, src => src.City)
.Map(dest => dest.PostalCode, src => src.PostalCode)
.Map(dest => dest.Region, src => src.Region)
.Map(dest => dest.Country, src => src.Country);
config.NewConfig<DataCenter, ServerLocation> ()
.Map (dest => dest.Id, src => src.Id)
.Map (dest => dest.Name, src => src.Name)
.Map (dest => dest.Address, src => src.Address)
.Map (dest => dest.City, src => src.City)
.Map (dest => dest.PostalCode, src => src.PostalCode)
.Map (dest => dest.Region, src => src.Region)
.Map (dest => dest.Country, src => src.Country)
.Map (dest => dest.Description, src => src.Description);

config.NewConfig<ServerLocation, DataCenter> ()
.Map (dest => dest.Id, src => src.Id)
Expand All @@ -20,6 +21,7 @@ public void Register (TypeAdapterConfig config)
.Map (dest => dest.City, src => src.City)
.Map (dest => dest.PostalCode, src => src.PostalCode)
.Map (dest => dest.Region, src => src.Region)
.Map (dest => dest.Country, src => src.Country);
.Map (dest => dest.Country, src => src.Country)
.Map( dest => dest.Description, src => src.Description);
}
}
58 changes: 58 additions & 0 deletions src/hiPower.Core/ServerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using hiPower.Abstracts;
using hiPower.Common.Type.Errors;
using MapsterMapper;

namespace hiPower.Core;

public class ServerService(IUnitOfWork unit,
IMapper mapper) : IServerService
{
public async Task<ErrorOr<Dto.Manager.Server>> CreateAsync (Dto.Manager.Server server)
{
var result = await unit.ServerRepository.CreateAsync(server.Adapt<Entity.Server>());
await unit.SaveAsync ();
return mapper.Map<Dto.Manager.Server> (result);
}

public async Task<ErrorOr<bool>> DeleteAsync (string id)
{
await unit.ServerRepository.DeleteAsync (id);
return true;
}

public async Task<ErrorOr<IEnumerable<Dto.Manager.Server>>> GetAllAsync (string dataCenterId)
{
var result = await unit.ServerRepository.GetAll(x => x.LocationId.Equals(dataCenterId.ToUpper()), null, null);
return result.Adapt<IEnumerable<Dto.Manager.Server>> ()
.ToErrorOr();
}

public async Task<ErrorOr<IEnumerable<Dto.Manager.Server>>> GetAllAsync ()
{
var result = await unit.ServerRepository.GetAll(null, null, null);
return result.Adapt<IEnumerable<Dto.Manager.Server>>()
.ToErrorOr();
}

public async Task<ErrorOr<Dto.Manager.Server>> GetAsync (string id)
{
var result = await unit.ServerRepository
.GetAsync(x => x.Id.Equals(id.ToUpper()), null);
if (result is not null)
{
return mapper.Map<Dto.Manager.Server> (result);
}
return Error.NotFound();
}

public async Task<ErrorOr<Dto.Manager.Server>> UpdateAsync (string id, Dto.Manager.Server server)
{
if (id.ToUpper ().Equals (server.Id.ToUpper ()))
{
var updateServer = server.Adapt<Entity.Server> ();
var result = unit.ServerRepository.Update(updateServer);
return await Task.FromResult(result.Adapt<Dto.Manager.Server>());
}
return ServerServiceErrors.UpdateError;
}
}
4 changes: 2 additions & 2 deletions src/hiPower.Repository/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class UnitOfWork(DbContextOptions<ManagerDbContext> options) : IUnitOfWor
private bool _disposed = false;
private readonly ManagerDbContext dbContext = new(options);

private IGenericRepository<ServerLocation>? locationRepository;
private IGenericRepository<ServerLocation>? dataCenterRepository;
private IGenericRepository<Server>? serverRepository;

public IGenericRepository<ServerLocation> LocationRepository => locationRepository ??= new GenericRepository<ServerLocation> (dbContext);
public IGenericRepository<ServerLocation> DataCenterRepository => dataCenterRepository ??= new GenericRepository<ServerLocation> (dbContext);

public IGenericRepository<Server> ServerRepository => serverRepository ??= new GenericRepository<Server> (dbContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace hiPower.WebApi.Controllers
[Route ("api/datacenters")]
[ApiController]
[Produces (MediaTypeNames.Application.Json)]
public class DataCenterController(IDataCenterService locationService) : ControllerBase
public class DataCentersController(IDataCenterService locationService) : ControllerBase
{
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK, Type= typeof (ApiResult<IEnumerable<DataCenter>>))]
Expand All @@ -20,6 +20,7 @@ public async Task<IActionResult> GetAll()
}

var response = new ApiResult<IEnumerable<DataCenter>>(true, result.Value);
Thread.Sleep (1000);
return Ok (response);
}

Expand All @@ -36,6 +37,7 @@ public async Task<IActionResult> Get ([FromRoute] string id)
{
return BadRequest(new ProblemDetails () { Status = StatusCodes.Status400BadRequest });
}
Thread.Sleep (1000);
return Ok (new ApiResult<DataCenter> (true, result.Value));
}

Expand Down
76 changes: 56 additions & 20 deletions src/hiPower.WebApi/Controllers/ServersController.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,82 @@
using Microsoft.AspNetCore.Mvc;
using hiPower.Abstracts;
using Microsoft.AspNetCore.Mvc;

namespace hiPower.WebApi.Controllers
{
[Route ("api/servers")]
[ApiController]
[Produces (MediaTypeNames.Application.Json)]
public class ServersController : ControllerBase
public class ServersController(IServerService serverService) : ControllerBase
{
[HttpGet]
[ProducesResponseType (StatusCodes.Status200OK, Type = typeof (ApiResult<IEnumerable<Server>>))]
public IActionResult GetAll ()
public async Task<IActionResult> GetAll ()
{
var response = new ApiResult<IEnumerable<Server>>(true, []);
var response = await serverService.GetAllAsync ();

return Ok (response);
if (response.IsError)
{
return BadRequest ();
}

return Ok (new ApiResult<IEnumerable<Server>>(!response.IsError, response.Value));
}


[HttpGet ("{id}")]
[ProducesResponseType (StatusCodes.Status200OK, Type = typeof (ApiResult<Server>))]
[ProducesResponseType (StatusCodes.Status400BadRequest, Type = typeof (ProblemDetails))]
[ProducesResponseType (StatusCodes.Status404NotFound, Type = typeof (ProblemDetails))]
public IActionResult Get ([FromRoute] string id)
public async Task<IActionResult> Get ([FromRoute] string id)
{
var result = await serverService.GetAsync (id);

if (result.IsError)
{
return NotFound ();
}

return Ok (new ApiResult<Server>(!result.IsError, result.Value));
}

[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(ApiResult<Server>))]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ProblemDetails))]
public async Task<IActionResult> Create ([FromBody] Server server)
{
var result = await serverService.CreateAsync(server);
if (result.IsError)
{
return BadRequest ();
}
return Created (string.Empty, new ApiResult<Server> (!result.IsError, result.Value));
}

[HttpPut ("{id}")]
[ProducesResponseType (StatusCodes.Status200OK, Type = typeof (ApiResult<Server>))]
[ProducesResponseType (StatusCodes.Status400BadRequest, Type = typeof (ProblemDetails))]
public async Task<IActionResult> Update ([FromRoute] string id, [FromBody] Server server)
{
var result = new ApiResult<Server>(true, new Server());
return Ok (result);
var result = await serverService.UpdateAsync(id, server);
if (result.IsError)
{
return BadRequest ();
}
return Ok (new ApiResult<Server> (!result.IsError, result.Value));
}

[HttpGet ("{id}/zones")]
[ProducesResponseType (StatusCodes.Status200OK, Type = typeof (ApiResult<IEnumerable<Zone>>))]
[HttpDelete("{id}")]
[ProducesResponseType (StatusCodes.Status200OK, Type = typeof (ApiResult<Server>))]
[ProducesResponseType (StatusCodes.Status400BadRequest, Type = typeof (ProblemDetails))]
[ProducesResponseType (StatusCodes.Status404NotFound, Type = typeof (ProblemDetails))]
public IActionResult GetZones ([FromRoute] string id)
public async Task<IActionResult> Deleted ([FromQuery] string id)
{
IEnumerable<Zone> zones = [
new Zone("example-1.eu",ZoneKind.Slave, ["127.0.0.1"], false, 2024051626),
new Zone("example-2.eu",ZoneKind.Slave, ["127.0.0.1"], false, 2024100116),
new Zone("example-3.pl",ZoneKind.Slave, ["127.0.0.1"], false, 2021090103),
];

var result = new ApiResult<IEnumerable<Zone>>(true, zones);
return Ok (result);
var result = await serverService.DeleteAsync(id);

if(result.IsError)
{
return BadRequest ();
}
return Ok ();
}
}
}
11 changes: 11 additions & 0 deletions src/hiPower.WebApi/Controllers/TemplatesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace hiPower.WebApi.Controllers
{
[Route ("api/templates")]
[ApiController]
public class TemplatesController : ControllerBase
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { ManagerErrorHandler } from '@shared/handlers/manager-error-handler';
import { DashboarRoutingModule } from './bashboard-routing.nodule';
import { LayoutComponent } from './layout/layout.component';
import { HomeComponent } from './home/home.component';
import { LoadingModule } from '@shared/components/loading/loading.module';
import { LoadingService } from '@shared/components/loading/loading.service';

@NgModule({
declarations: [
Expand All @@ -24,9 +26,11 @@ import { HomeComponent } from './home/home.component';
MatToolbarModule,
DashboarRoutingModule,
SharedModule,
MaterialsModule
],
MaterialsModule,
LoadingModule
],
providers: [
LoadingService,
{
provide: ErrorHandler,
useClass: ManagerErrorHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ActivatedRouteSnapshot, ResolveFn, RouterStateSnapshot } from "@angular/router";
import { DataCenter } from "../models/data-center";
import { map } from 'rxjs';
import { inject } from "@angular/core";
import { DataCenterService } from "../../services/data-center.service";
import { EMPTY_DATA_CENTER } from "../models/empty-data-center";
Expand Down
Loading

0 comments on commit e570a1b

Please sign in to comment.