Skip to content

Commit

Permalink
chore(address): rework the dto and applied to the providers, controller
Browse files Browse the repository at this point in the history
  • Loading branch information
simosbe3 committed Nov 28, 2024
1 parent 10baa4c commit 6eda998
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 82 deletions.
11 changes: 6 additions & 5 deletions api/Controllers/SuppliersController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using DTO.Supplier;
using DTO.Address;

[ApiController]
[Route("api/[controller]")]
Expand Down Expand Up @@ -34,7 +35,7 @@ public IActionResult Create(SupplierRequest request)
Email = supplier.Contact.Email,
Phone = supplier.Contact.Phone
},
Address = new AddressDTO
Address = new AddressResponse
{
Street = supplier.Address.Street,
HouseNumber = supplier.Address.HouseNumber,
Expand Down Expand Up @@ -72,7 +73,7 @@ public IActionResult Update(Guid id, SupplierRequest request)
Email = updatedSupplier.Contact.Email,
Phone = updatedSupplier.Contact.Phone
},
Address = new AddressDTO
Address = new AddressResponse
{
Street = updatedSupplier.Address.Street,
HouseNumber = updatedSupplier.Address.HouseNumber,
Expand Down Expand Up @@ -106,7 +107,7 @@ public IActionResult Delete(Guid id)
Email = deletedSupplier.Contact.Email,
Phone = deletedSupplier.Contact.Phone
},
Address = new AddressDTO
Address = new AddressResponse
{
Street = deletedSupplier.Address.Street,
HouseNumber = deletedSupplier.Address.HouseNumber,
Expand Down Expand Up @@ -141,7 +142,7 @@ public IActionResult ShowSingle(Guid id)
Email = supplier.Contact.Email,
Phone = supplier.Contact.Phone
},
Address = new AddressDTO
Address = new AddressResponse
{
Street = supplier.Address.Street,
HouseNumber = supplier.Address.HouseNumber,
Expand Down Expand Up @@ -169,7 +170,7 @@ public IActionResult ShowSingle(Guid id)
Email = ig.Contact.Email,
Phone = ig.Contact.Phone
},
Address = new AddressDTO
Address = new AddressResponse
{
Street = ig.Address.Street,
HouseNumber = ig.Address.HouseNumber,
Expand Down
96 changes: 66 additions & 30 deletions api/DTOs/AddressDTO.cs
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; }
}
}
5 changes: 3 additions & 2 deletions api/DTOs/SupplierDTO.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using DTO.Address;
using Microsoft.AspNetCore.Mvc;

namespace DTO.Supplier;
Expand All @@ -25,7 +26,7 @@ public class SupplierRequest : BaseDTO
public Guid? AddressId { get; set; }

[JsonPropertyName("address")]
public AddressDTO? Address { get; set; }
public AddressRequest? Address { get; set; }
}

[ApiExplorerSettings(IgnoreApi = true)]
Expand All @@ -47,7 +48,7 @@ public class SupplierResponse : BaseDTO
public ContactDTO? Contact { get; set; }

[JsonPropertyName("address")]
public AddressDTO? Address { get; set; }
public AddressResponse? Address { get; set; }

[JsonPropertyName("created_at")]
public DateTime? CreatedAt { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion api/DTOs/WarehouseDTO.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using DTO.Address;

namespace DTOs;
public class WarehouseDTO : IDTO
Expand All @@ -20,7 +21,7 @@ public class WarehouseDTO : IDTO
public Guid? AddressId { get; set; }

[JsonPropertyName("address")]
public AddressDTO? Address { get; set; }
public AddressRequest? Address { get; set; }
}


Expand Down
113 changes: 70 additions & 43 deletions api/providers/AddressProvider.cs
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;
}
}
2 changes: 1 addition & 1 deletion api/providers/WarehouseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public WarehouseProvider(AppDbContext db, AddressProvider addressProvider, Conta
?? throw new ApiFlowException("address_id does not exist");

return request.Address != null
? _addressProvider.Create<AddressDTO>(request.Address)
? _addressProvider.Create(request.Address)
: throw new ApiFlowException("An error occurred while saving the warehouse address");
}

Expand Down

0 comments on commit 6eda998

Please sign in to comment.