-
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.
chore(address): rework the dto and applied to the providers, controller
- Loading branch information
Showing
6 changed files
with
148 additions
and
82 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
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,33 +1,69 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Text.Json.Serialization; | ||
public class AddressDTO : IDTO | ||
{ | ||
[Required] | ||
[JsonPropertyName("street")] | ||
public required string Street { get; set; } | ||
|
||
[Required] | ||
[JsonPropertyName("house_number")] | ||
public required string HouseNumber { get; set; } | ||
|
||
[JsonPropertyName("house_number_extension")] | ||
public string? HouseNumberExtension { get; set; } | ||
|
||
[JsonPropertyName("house_number_extension_extra")] | ||
public string? HouseNumberExtensionExtra { get; set; } | ||
|
||
[Required] | ||
[JsonPropertyName("zipcode")] | ||
public required string ZipCode { get; set; } | ||
|
||
[Required] | ||
[JsonPropertyName("city")] | ||
public required string City { get; set; } | ||
|
||
[JsonPropertyName("province")] | ||
public string? Province { get; set; } | ||
|
||
[Required] | ||
[JsonPropertyName("country_code")] | ||
public required string CountryCode { get; set; } | ||
namespace DTO.Address | ||
{ | ||
[ApiExplorerSettings(IgnoreApi = true)] | ||
public class AddressRequest : BaseDTO | ||
{ | ||
[JsonPropertyName("street")] | ||
public string? Street { get; set; } | ||
|
||
[JsonPropertyName("house_number")] | ||
public string? HouseNumber { get; set; } | ||
|
||
[JsonPropertyName("house_number_extension")] | ||
public string? HouseNumberExtension { get; set; } | ||
|
||
[JsonPropertyName("house_number_extension_extra")] | ||
public string? HouseNumberExtensionExtra { get; set; } | ||
|
||
[JsonPropertyName("zipcode")] | ||
public string? ZipCode { get; set; } | ||
|
||
[JsonPropertyName("city")] | ||
public string? City { get; set; } | ||
|
||
[JsonPropertyName("province")] | ||
public string? Province { get; set; } | ||
|
||
[JsonPropertyName("country_code")] | ||
public string? CountryCode { get; set; } | ||
} | ||
[ApiExplorerSettings(IgnoreApi = true)] | ||
public class AddressResponse : BaseDTO | ||
{ | ||
[JsonPropertyName("id")] | ||
public Guid Id { get; set; } | ||
|
||
[JsonPropertyName("street")] | ||
public string? Street { get; set; } | ||
|
||
[JsonPropertyName("house_number")] | ||
public string? HouseNumber { get; set; } | ||
|
||
[JsonPropertyName("house_number_extension")] | ||
public string? HouseNumberExtension { get; set; } | ||
|
||
[JsonPropertyName("house_number_extension_extra")] | ||
public string? HouseNumberExtensionExtra { get; set; } | ||
|
||
[JsonPropertyName("zipcode")] | ||
public string? ZipCode { get; set; } | ||
|
||
[JsonPropertyName("city")] | ||
public string? City { get; set; } | ||
|
||
[JsonPropertyName("province")] | ||
public string? Province { get; set; } | ||
|
||
[JsonPropertyName("country_code")] | ||
public string? CountryCode { get; set; } | ||
|
||
[JsonPropertyName("created_at")] | ||
public DateTime? CreatedAt { get; set; } | ||
|
||
[JsonPropertyName("updated_at")] | ||
public DateTime? UpdatedAt { get; set; } | ||
} | ||
} |
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,67 +1,94 @@ | ||
public class AddressProvider : ICRUD<Address> | ||
using DTO.Address; | ||
using FluentValidation; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class AddressProvider : BaseProvider<Address> | ||
{ | ||
private readonly AppDbContext _db; | ||
private readonly IValidator<Address> _addressValidator; | ||
|
||
public AddressProvider(AppDbContext db) | ||
public AddressProvider(AppDbContext db, IValidator<Address> validator) : base(db) | ||
{ | ||
_db = db; | ||
_addressValidator = validator; | ||
} | ||
|
||
public Address? Create<IDTO>(IDTO newElement) | ||
public override List<Address> GetAll() => _db.Addresses.ToList(); | ||
|
||
public override Address? GetById(Guid id) => | ||
_db.Addresses.FirstOrDefault(a => a.Id == id); | ||
|
||
public override Address? Create(BaseDTO createValues) | ||
{ | ||
AddressDTO? request = newElement as AddressDTO; | ||
if(request == null) throw new Exception("Request invalid"); | ||
|
||
Address newAddress = new(){ | ||
Street=request.Street, | ||
HouseNumber=request.HouseNumber, | ||
HouseNumberExtension=request.HouseNumberExtension, | ||
HouseNumberExtensionExtra=request.HouseNumberExtensionExtra, | ||
ZipCode=request.ZipCode, | ||
City=request.City, | ||
Province=request.Province, | ||
CountryCode=request.CountryCode, | ||
CreatedAt=DateTime.UtcNow | ||
}; | ||
AddressRequest? req = createValues as AddressRequest; | ||
if (req == null) throw new ApiFlowException("Invalid address request. Could not create address."); | ||
|
||
_db.Addresses.Add(newAddress); | ||
Address newAddress = new Address(newInstance: true) | ||
{ | ||
Street = req.Street, | ||
HouseNumber = req.HouseNumber, | ||
HouseNumberExtension = req.HouseNumberExtension, | ||
HouseNumberExtensionExtra = req.HouseNumberExtensionExtra, | ||
ZipCode = req.ZipCode, | ||
City = req.City, | ||
Province = req.Province, | ||
CountryCode = req.CountryCode, | ||
CreatedAt = DateTime.UtcNow | ||
}; | ||
|
||
DBUtil.SaveChanges(_db, "Address not stored"); | ||
ValidateModel(newAddress); | ||
|
||
_db.Addresses.Add(newAddress); | ||
SaveToDBOrFail(); | ||
|
||
return newAddress; | ||
|
||
} | ||
|
||
public Address Delete(Guid id) | ||
public override Address? Delete(Guid id) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
Address? foundAddress = _db.Addresses.FirstOrDefault(a => a.Id == id); | ||
if (foundAddress == null) return null; | ||
|
||
public List<Address> GetAll() | ||
{ | ||
throw new NotImplementedException(); | ||
_db.Addresses.Remove(foundAddress); | ||
SaveToDBOrFail(); | ||
|
||
return foundAddress; | ||
} | ||
|
||
|
||
public Address? GetById(Guid id) | ||
public override Address? Update(Guid id, BaseDTO updateValues) | ||
{ | ||
return _db.Addresses.FirstOrDefault(a => a.Id == id); | ||
AddressRequest? req = updateValues as AddressRequest; | ||
if (req == null) throw new ApiFlowException("Invalid address request. Could not update address."); | ||
|
||
Address? existingAddress = _db.Addresses.FirstOrDefault(a => a.Id == id); | ||
if (existingAddress == null) throw new ApiFlowException($"Address not found for id '{id}'"); | ||
|
||
existingAddress.Street = req.Street; | ||
existingAddress.HouseNumber = req.HouseNumber; | ||
existingAddress.HouseNumberExtension = req.HouseNumberExtension; | ||
existingAddress.HouseNumberExtensionExtra = req.HouseNumberExtensionExtra; | ||
existingAddress.ZipCode = req.ZipCode; | ||
existingAddress.City = req.City; | ||
existingAddress.Province = req.Province; | ||
existingAddress.CountryCode = req.CountryCode; | ||
existingAddress.UpdatedAt = DateTime.UtcNow; | ||
|
||
ValidateModel(existingAddress); | ||
|
||
_db.Addresses.Update(existingAddress); | ||
SaveToDBOrFail(); | ||
|
||
return existingAddress; | ||
} | ||
|
||
public Address? Update<IDTO>(Guid id, IDTO dto) | ||
protected override void ValidateModel(Address model) => _addressValidator.ValidateAndThrow(model); | ||
|
||
public Address? GetOrCreateAddress(AddressRequest? address = null, Guid? addressId = null) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Address? GetOrCreateAddress(AddressDTO? addressDTO = null, Guid? addressId = null) | ||
{ | ||
if (addressDTO == null && addressId == null) return null; | ||
if (address == null && addressId == null) return null; | ||
|
||
if (addressId != null) return GetById(addressId.Value); | ||
if (addressId != null) return GetById(addressId.Value); | ||
|
||
if(addressDTO != null) return Create(addressDTO); | ||
if (address != null) return Create(address); | ||
|
||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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