Skip to content

Commit

Permalink
chore(contact): 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 6a26e1a commit 618d383
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 56 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.Contact;

[ApiController]
[Route("api/[controller]")]
Expand Down Expand Up @@ -28,7 +29,7 @@ public IActionResult Create(SupplierRequest request)
Code = supplier.Code,
Name = supplier.Name,
Reference = supplier.Reference,
Contact = new ContactDTO
Contact = new ContactResponse
{
Name = supplier.Contact.Name,
Email = supplier.Contact.Email,
Expand Down Expand Up @@ -66,7 +67,7 @@ public IActionResult Update(Guid id, SupplierRequest request)
Code = updatedSupplier.Code,
Name = updatedSupplier.Name,
Reference = updatedSupplier.Reference,
Contact = new ContactDTO
Contact = new ContactResponse
{
Name = updatedSupplier.Contact.Name,
Email = updatedSupplier.Contact.Email,
Expand Down Expand Up @@ -100,7 +101,7 @@ public IActionResult Delete(Guid id)
Code = deletedSupplier.Code,
Name = deletedSupplier.Name,
Reference = deletedSupplier.Reference,
Contact = new ContactDTO
Contact = new ContactResponse
{
Name = deletedSupplier.Contact.Name,
Email = deletedSupplier.Contact.Email,
Expand Down Expand Up @@ -135,7 +136,7 @@ public IActionResult ShowSingle(Guid id)
Name = supplier.Name,
Code = supplier.Code,
Reference = supplier.Reference,
Contact = new ContactDTO
Contact = new ContactResponse
{
Name = supplier.Contact.Name,
Email = supplier.Contact.Email,
Expand Down Expand Up @@ -163,7 +164,7 @@ public IActionResult ShowSingle(Guid id)
Code = ig.Code,
Name = ig.Name,
Reference = ig.Reference,
Contact = new ContactDTO
Contact = new ContactResponse
{
Name = ig.Contact.Name,
Email = ig.Contact.Email,
Expand Down
50 changes: 37 additions & 13 deletions api/DTOs/ContactDTO.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json.Serialization;

public class ContactDTO : IDTO
namespace DTO.Contact
{
[Required]
[JsonPropertyName("name")]
public required string Name { get; set; }
[ApiExplorerSettings(IgnoreApi = true)]
public class ContactRequest : BaseDTO
{
[JsonPropertyName("name")]
public string? Name { get; set; }

[JsonPropertyName("phone")]
public string? Phone { get; set; }

[JsonPropertyName("email")]
public string? Email { get; set; }
}


[Required]
[JsonPropertyName("phone")]
public required string Phone { get; set; }

[Required]
[JsonPropertyName("email")]
public required string Email { get; set; }
}
[ApiExplorerSettings(IgnoreApi = true)]
public class ContactResponse : BaseDTO
{
[JsonPropertyName("id")]
public Guid Id { get; set; }

[JsonPropertyName("name")]
public string? Name { get; set; }

[JsonPropertyName("phone")]
public string? Phone { get; set; }

[JsonPropertyName("email")]
public string? Email { 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.Contact;
using Microsoft.AspNetCore.Mvc;

namespace DTO.Supplier;
Expand All @@ -19,7 +20,7 @@ public class SupplierRequest : BaseDTO
public Guid? ContactId { get; set; }

[JsonPropertyName("contact")]
public ContactDTO? Contact { get; set; }
public ContactRequest? Contact { get; set; }

[JsonPropertyName("address_id")]
public Guid? AddressId { get; set; }
Expand All @@ -44,7 +45,7 @@ public class SupplierResponse : BaseDTO
public string? Reference { get; set; }

[JsonPropertyName("contact")]
public ContactDTO? Contact { get; set; }
public ContactResponse? Contact { get; set; }

[JsonPropertyName("address")]
public AddressDTO? Address { 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.Contact;

namespace DTOs;
public class WarehouseDTO : IDTO
Expand All @@ -14,7 +15,7 @@ public class WarehouseDTO : IDTO
public Guid? ContactId { get; set; }

[JsonPropertyName("contact")]
public ContactDTO? Contact { get; set; }
public ContactRequest? Contact { get; set; }

[JsonPropertyName("address_id")]
public Guid? AddressId { get; set; }
Expand Down
93 changes: 59 additions & 34 deletions api/providers/ContactProvider.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,84 @@
public class ContactProvider : ICRUD<Contact>
using DTO.Contact;
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using Utils.Date;

public class ContactProvider : BaseProvider<Contact>
{
private readonly AppDbContext _db;
private IValidator<Contact> _contactValidator;

public ContactProvider(AppDbContext db)
{
_db = db;
}
public List<Contact> GetAll()
public ContactProvider(AppDbContext db, IValidator<Contact> validator) : base(db)
{
return _db.Contacts.ToList();
_contactValidator = validator;
}

public Contact? Create<IDTO>(IDTO newElement)
public override List<Contact> GetAll() => _db.Contacts.ToList();

public override Contact? GetById(Guid id) =>
_db.Contacts.FirstOrDefault(c => c.Id == id);

public override Contact? Create(BaseDTO createValues)
{
ContactDTO? request = newElement as ContactDTO;
if(request == null) throw new Exception("Request invalid");
ContactRequest? req = createValues as ContactRequest;
if (req == null) throw new ApiFlowException("Invalid contact request. Could not create contact.");

Contact newContact = new()
Contact newContact = new Contact(newInstance: true)
{
Name = request.Name,
Email = request.Email,
Phone = request.Phone,
CreatedAt = DateTime.UtcNow,
Name = req.Name,
Phone = req.Phone,
Email = req.Email
};

_db.Contacts.Add(newContact);
ValidateModel(newContact);

DBUtil.SaveChanges(_db, "Contact not stored");
_db.Contacts.Add(newContact);
SaveToDBOrFail();

return newContact;
return newContact;
}

public Contact Delete(Guid id)
public override Contact? Delete(Guid id)
{
throw new NotImplementedException();
}
Contact? foundContact = _db.Contacts.FirstOrDefault(c => c.Id == id);
if (foundContact == null) return null;

public Contact? GetById(Guid id)
{
return _db.Contacts.FirstOrDefault(c => c.Id == id);
_db.Contacts.Remove(foundContact);
SaveToDBOrFail();

return foundContact;
}

public Contact? Update<IDTO>(Guid id, IDTO dto)
public override Contact? Update(Guid id, BaseDTO updateValues)
{
throw new NotImplementedException();
ContactRequest? req = updateValues as ContactRequest;
if (req == null) throw new ApiFlowException("Invalid contact request. Could not update contact.");

Contact? existingContact = _db.Contacts.FirstOrDefault(c => c.Id == id);
if (existingContact == null) throw new ApiFlowException($"Contact not found for id '{id}'");

existingContact.Name = req.Name;
existingContact.Phone = req.Phone;
existingContact.Email = req.Email;

ValidateModel(existingContact);

_db.Contacts.Update(existingContact);
SaveToDBOrFail();

return existingContact;
}

public Contact? GetOrCreateContact(ContactDTO? contact = null, Guid? contactId = null)
{
if (contact == null && contactId == null) return null;
protected override void ValidateModel(Contact model) => _contactValidator.ValidateAndThrow(model);

// Andere methoden zoals GetOrCreateContact blijven zoals ze zijn
public Contact? GetOrCreateContact(ContactRequest? contact = null, Guid? contactId = null)
{
if (contact == null && contactId == null) return null;

if (contactId != null) return GetById(contactId.Value);
if (contactId != null) return GetById(contactId.Value);

if(contact != null) return Create(contact);
if (contact != null) return Create(contact);

return null;
}
return null;
}
}
3 changes: 2 additions & 1 deletion api/providers/WarehouseProvider.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using DTO.Contact;
using DTOs;

public class WarehouseProvider : ICRUD<Warehouse>
Expand Down Expand Up @@ -51,7 +52,7 @@ public WarehouseProvider(AppDbContext db, AddressProvider addressProvider, Conta
?? throw new ApiFlowException("contact_id does not exist");

return request.Contact != null
? _contactProvider.Create<ContactDTO>(request.Contact)
? _contactProvider.Create(request.Contact)
: throw new ApiFlowException("An error occurred while saving the warehouse contact");
}

Expand Down

0 comments on commit 618d383

Please sign in to comment.