-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Loading servie, Create, List, Edit
- Loading branch information
Adam Dębowski
committed
Dec 2, 2024
1 parent
47c957e
commit e570a1b
Showing
35 changed files
with
538 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...ns-manager/src/app/features/dashboard/data-centers/core/resolvers/data-center.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.