fix: Result & DTOs for GetById + cleanup
This commit is contained in:
@@ -29,10 +29,18 @@ public class TicketsController : ControllerBase
|
|||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public ActionResult<TicketResponse> Get(Guid id)
|
public ActionResult<TicketResponse> Get(Guid id)
|
||||||
{
|
{
|
||||||
var ticket = _service.GetById(id);
|
var result = _service.GetById(id);
|
||||||
|
|
||||||
|
if (result is FailureResult<TicketResponse> fail)
|
||||||
|
return NotFound(fail.Error);
|
||||||
|
|
||||||
|
var success = (SuccessResult<TicketResponse>) result;
|
||||||
|
return Ok(success.Value);
|
||||||
|
|
||||||
|
/*
|
||||||
if (ticket == null) return NotFound();
|
if (ticket == null) return NotFound();
|
||||||
return Ok(ticket);
|
return Ok(ticket);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
|
|||||||
@@ -33,22 +33,3 @@ public sealed class FailureResult<T> : Result<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
private Result(bool success, T? value, string? error)
|
|
||||||
{
|
|
||||||
Success = success;
|
|
||||||
Value = value;
|
|
||||||
Error = error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Result<T> Ok(T value)
|
|
||||||
=> new(true,value,null);
|
|
||||||
|
|
||||||
public static Result<T> Fail(string error)
|
|
||||||
=> new(false, default, error);
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
@@ -16,3 +16,12 @@ public class CreateTicketResponse
|
|||||||
public string Title{get;set;} = "";
|
public string Title{get;set;} = "";
|
||||||
public string Description{get;set;} = "";
|
public string Description{get;set;} = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class TicketResponse
|
||||||
|
{
|
||||||
|
public Guid Id {get;set;}
|
||||||
|
public string Title{get;set;} = "";
|
||||||
|
public string Description{get;set;} = "";
|
||||||
|
|
||||||
|
}
|
||||||
@@ -9,6 +9,6 @@ public interface ITicketService
|
|||||||
//TODO: implement Repository
|
//TODO: implement Repository
|
||||||
IEnumerable<TicketResponse> GetAll();
|
IEnumerable<TicketResponse> GetAll();
|
||||||
|
|
||||||
Ticket? GetById(Guid id);
|
Result<TicketResponse> GetById(Guid id);
|
||||||
Result<CreateTicketResponse> Create(CreateTicketRequest request);
|
Result<CreateTicketResponse> Create(CreateTicketRequest request);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.ExceptionServices;
|
||||||
using Microsoft.AspNetCore.Http.HttpResults;
|
using Microsoft.AspNetCore.Http.HttpResults;
|
||||||
|
using Microsoft.Extensions.ObjectPool;
|
||||||
using TicketAppIncrArchi.Application.DTO;
|
using TicketAppIncrArchi.Application.DTO;
|
||||||
using TicketAppIncrArchi.Application.Interfaces;
|
using TicketAppIncrArchi.Application.Interfaces;
|
||||||
using TicketAppIncrArchi.Domain.Entities;
|
using TicketAppIncrArchi.Domain.Entities;
|
||||||
@@ -23,9 +25,24 @@ public class TicketService : ITicketService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Ticket? GetById(Guid id)
|
public Result<TicketResponse> GetById(Guid id)
|
||||||
{
|
{
|
||||||
return _tickets.FirstOrDefault(ticket => ticket.Id == id);
|
//TODO: should use result
|
||||||
|
var found = _tickets.FirstOrDefault(ticket => ticket.Id == id);
|
||||||
|
|
||||||
|
if (found == null)
|
||||||
|
{
|
||||||
|
return Result<TicketResponse>.Fail("No Ticket Found");
|
||||||
|
}
|
||||||
|
|
||||||
|
var ticketResponse = new TicketResponse
|
||||||
|
{
|
||||||
|
Id = found.Id,
|
||||||
|
Title = found.Title,
|
||||||
|
Description = found.Description,
|
||||||
|
};
|
||||||
|
|
||||||
|
return Result<TicketResponse>.Ok(found);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result<CreateTicketResponse> Create(CreateTicketRequest request)
|
public Result<CreateTicketResponse> Create(CreateTicketRequest request)
|
||||||
@@ -44,14 +61,14 @@ public class TicketService : ITicketService
|
|||||||
//send creation to repo
|
//send creation to repo
|
||||||
_tickets.Add(ticket);
|
_tickets.Add(ticket);
|
||||||
|
|
||||||
var response = new CreateTicketResponse
|
var ticketResponse = new CreateTicketResponse
|
||||||
{
|
{
|
||||||
Id = ticket.Id,
|
Id = ticket.Id,
|
||||||
Title = ticket.Title,
|
Title = ticket.Title,
|
||||||
Description = ticket.Description
|
Description = ticket.Description
|
||||||
};
|
};
|
||||||
|
|
||||||
return Result<CreateTicketResponse>.Ok(response);
|
return Result<CreateTicketResponse>.Ok(ticketResponse);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,5 +4,4 @@ namespace TicketAppIncrArchi.Domain.Entities;
|
|||||||
public Guid Id {get;set;}
|
public Guid Id {get;set;}
|
||||||
public string Title {get;set;} = "";
|
public string Title {get;set;} = "";
|
||||||
public string Description {get;set;} = "";
|
public string Description {get;set;} = "";
|
||||||
//public string Status {get;set;} = "";
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user