Introduce dtos
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
using GerbilManager.Models;
|
using GerbilManager.Models;
|
||||||
using GerbilManager.Repositories;
|
using GerbilManager.Repositories;
|
||||||
|
using GerbilManager.Dtos;
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using GerbilManager.Converter;
|
||||||
|
|
||||||
namespace GerbilManager.Controllers
|
namespace GerbilManager.Controllers
|
||||||
{
|
{
|
||||||
@@ -17,17 +20,17 @@ namespace GerbilManager.Controllers
|
|||||||
|
|
||||||
//GET /gerbils
|
//GET /gerbils
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IEnumerable<Gerbil> GetGerbils()
|
public IEnumerable<GerbilDto> GetGerbils()
|
||||||
{
|
{
|
||||||
var items = repository.GetGerbils();
|
var items = repository.GetGerbils().Select( gerbil => gerbil.AsDto());
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
//GET /gerbils
|
//GET /gerbils
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public ActionResult<Gerbil> GetGerbil(Guid id)
|
public ActionResult<GerbilDto> GetGerbil(Guid id)
|
||||||
{
|
{
|
||||||
var item = repository.GetGerbil(id);
|
var item = repository.GetGerbil(id).AsDto();
|
||||||
|
|
||||||
if(item is null)
|
if(item is null)
|
||||||
{
|
{
|
||||||
|
|||||||
17
Converter/BreederConveter.cs
Normal file
17
Converter/BreederConveter.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using GerbilManager.Dtos;
|
||||||
|
using GerbilManager.Models;
|
||||||
|
|
||||||
|
namespace GerbilManager.Converter
|
||||||
|
{
|
||||||
|
public static class BreederConverterExtension
|
||||||
|
{
|
||||||
|
public static BreederDto AsDto(this Breeder breeder)
|
||||||
|
{
|
||||||
|
return new BreederDto{
|
||||||
|
Id = breeder.Id,
|
||||||
|
Name = breeder.Name
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
19
Converter/GerbilConverter.cs
Normal file
19
Converter/GerbilConverter.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using GerbilManager.Dtos;
|
||||||
|
using GerbilManager.Models;
|
||||||
|
|
||||||
|
namespace GerbilManager.Converter
|
||||||
|
{
|
||||||
|
public static class GerbilConverterExtension
|
||||||
|
{
|
||||||
|
public static GerbilDto AsDto(this Gerbil gerbil)
|
||||||
|
{
|
||||||
|
return new GerbilDto{
|
||||||
|
Breeder = gerbil.Breeder.AsDto(),
|
||||||
|
Gender = (GenderDto) ((int) gerbil.Gender),
|
||||||
|
Name = gerbil.Name,
|
||||||
|
Litter = gerbil.Litter.AsDto()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
21
Converter/LitterConverter.cs
Normal file
21
Converter/LitterConverter.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using GerbilManager.Dtos;
|
||||||
|
using GerbilManager.Models;
|
||||||
|
|
||||||
|
namespace GerbilManager.Converter
|
||||||
|
{
|
||||||
|
public static class LitterConverterExtension
|
||||||
|
{
|
||||||
|
public static LitterDto AsDto(this Litter litter)
|
||||||
|
{
|
||||||
|
return new LitterDto{
|
||||||
|
Id = litter.Id,
|
||||||
|
Date = litter.Date,
|
||||||
|
Father = litter.Father.AsDto(),
|
||||||
|
Mother = litter.Mother.AsDto(),
|
||||||
|
Name = litter.Name,
|
||||||
|
Strength = litter.Strength
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
8
Dtos/BreederDto.cs
Normal file
8
Dtos/BreederDto.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace GerbilManager.Dtos
|
||||||
|
{
|
||||||
|
public record BreederDto
|
||||||
|
{
|
||||||
|
public Guid Id { get; init; }
|
||||||
|
public string Name { get; init; }
|
||||||
|
}
|
||||||
|
}
|
||||||
9
Dtos/GenderDto.cs
Normal file
9
Dtos/GenderDto.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace GerbilManager.Dtos
|
||||||
|
{
|
||||||
|
public enum GenderDto
|
||||||
|
{
|
||||||
|
unknown = 0,
|
||||||
|
male = 1,
|
||||||
|
female = 2
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Dtos/GerbilDto.cs
Normal file
11
Dtos/GerbilDto.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
namespace GerbilManager.Dtos
|
||||||
|
{
|
||||||
|
public record GerbilDto
|
||||||
|
{
|
||||||
|
public Guid Id { get; init; }
|
||||||
|
public string Name { get; init; }
|
||||||
|
public GenderDto Gender { get; init; }
|
||||||
|
public LitterDto Litter { get; init; }
|
||||||
|
public BreederDto Breeder { get; init; }
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Dtos/LitterDto.cs
Normal file
12
Dtos/LitterDto.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
namespace GerbilManager.Dtos
|
||||||
|
{
|
||||||
|
public record LitterDto
|
||||||
|
{
|
||||||
|
public Guid Id { get; init; }
|
||||||
|
public string Name { get; init; }
|
||||||
|
public DateTime Date { get; init; }
|
||||||
|
public int Strength { get; init; }
|
||||||
|
public GerbilDto Father { get; init; }
|
||||||
|
public GerbilDto Mother { get; init; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,8 @@ namespace GerbilManager.Models
|
|||||||
{
|
{
|
||||||
public enum Gender
|
public enum Gender
|
||||||
{
|
{
|
||||||
unknown,
|
unknown = 0,
|
||||||
male,
|
male = 1,
|
||||||
female
|
female = 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user