properly mapped the updated Dto to contact table
This commit is contained in:
parent
a00ef4313a
commit
eac2c31ea4
@ -23,7 +23,7 @@ namespace Marco.Pms.Model.Mapper
|
||||
TenantId = tenantId
|
||||
};
|
||||
}
|
||||
public static Contact ToContactFromUpdateContactDto(this UpdateContactDto updateContactDto, Guid tenantId)
|
||||
public static Contact ToContactFromUpdateContactDto(this UpdateContactDto updateContactDto, Guid tenantId, Contact contact)
|
||||
{
|
||||
|
||||
return new Contact
|
||||
@ -31,6 +31,8 @@ namespace Marco.Pms.Model.Mapper
|
||||
Id = updateContactDto.Id,
|
||||
Name = updateContactDto.Name ?? string.Empty,
|
||||
ContactCategoryId = updateContactDto.ContactCategoryId,
|
||||
CreatedAt = contact.CreatedAt,
|
||||
CreatedById = contact.CreatedById,
|
||||
Description = updateContactDto.Description ?? string.Empty,
|
||||
Organization = updateContactDto?.Organization ?? string.Empty,
|
||||
Address = updateContactDto != null ? updateContactDto.Address : string.Empty,
|
||||
|
@ -1724,15 +1724,16 @@ namespace Marco.Pms.Services.Helpers
|
||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended different ID in payload and path parameter", LoggedInEmployee.Id);
|
||||
return ApiResponse<object>.ErrorResponse("Invalid data", "Invalid data", 400);
|
||||
}
|
||||
Contact? contact = await _context.Contacts.FirstOrDefaultAsync(c => c.Id == id && c.IsActive && c.TenantId == tenantId);
|
||||
Contact? contact = await _context.Contacts.AsNoTracking().FirstOrDefaultAsync(c => c.Id == id && c.IsActive && c.TenantId == tenantId);
|
||||
if (contact == null)
|
||||
{
|
||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} tries to update contact with ID {ContactId} is not found in database", LoggedInEmployee.Id);
|
||||
return ApiResponse<object>.ErrorResponse("Contact not found", "Contact not found", 404);
|
||||
}
|
||||
|
||||
var newContact = updateContact.ToContactFromUpdateContactDto(tenantId);
|
||||
var newContact = updateContact.ToContactFromUpdateContactDto(tenantId, contact);
|
||||
_context.Contacts.Update(newContact);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
List<ContactPhone> phones = await _context.ContactsPhones.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync();
|
||||
var phoneIds = phones.Select(p => p.Id).ToList();
|
||||
@ -1844,7 +1845,8 @@ namespace Marco.Pms.Services.Helpers
|
||||
_context.ContactProjectMappings.Add(new ContactProjectMapping
|
||||
{
|
||||
ProjectId = ProjectId,
|
||||
ContactId = contact.Id
|
||||
ContactId = contact.Id,
|
||||
TenantId = tenantId
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1911,7 +1913,19 @@ namespace Marco.Pms.Services.Helpers
|
||||
UpdateAt = DateTime.UtcNow
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException dbEx)
|
||||
{
|
||||
|
||||
return ApiResponse<object>.ErrorResponse("User Send empty Payload", new
|
||||
{
|
||||
message = dbEx.Message,
|
||||
innerexcption = dbEx.InnerException.Message
|
||||
}, 400);
|
||||
}
|
||||
|
||||
contact = await _context.Contacts.Include(c => c.ContactCategory).FirstOrDefaultAsync(c => c.Id == id && c.IsActive && c.TenantId == tenantId) ?? new Contact();
|
||||
phones = await _context.ContactsPhones.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync();
|
||||
|
Loading…
x
Reference in New Issue
Block a user