-
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(contact): rework the dto and applied to the providers, controller
- Loading branch information
Showing
6 changed files
with
109 additions
and
56 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,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; } | ||
} | ||
} |
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,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; | ||
} | ||
} |
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