properly mapped the updated Dto to contact table

This commit is contained in:
ashutosh.nehete 2025-05-17 12:41:02 +05:30
parent 85d7863fb2
commit f42ebe3886
2 changed files with 21 additions and 5 deletions

View File

@ -23,7 +23,7 @@ namespace Marco.Pms.Model.Mapper
TenantId = tenantId 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 return new Contact
@ -31,6 +31,8 @@ namespace Marco.Pms.Model.Mapper
Id = updateContactDto.Id, Id = updateContactDto.Id,
Name = updateContactDto.Name ?? string.Empty, Name = updateContactDto.Name ?? string.Empty,
ContactCategoryId = updateContactDto.ContactCategoryId, ContactCategoryId = updateContactDto.ContactCategoryId,
CreatedAt = contact.CreatedAt,
CreatedById = contact.CreatedById,
Description = updateContactDto.Description ?? string.Empty, Description = updateContactDto.Description ?? string.Empty,
Organization = updateContactDto?.Organization ?? string.Empty, Organization = updateContactDto?.Organization ?? string.Empty,
Address = updateContactDto != null ? updateContactDto.Address : string.Empty, Address = updateContactDto != null ? updateContactDto.Address : string.Empty,

View File

@ -385,15 +385,16 @@ namespace Marco.Pms.Services.Helpers
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended different ID in payload and path parameter", LoggedInEmployee.Id); _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); 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) if (contact == null)
{ {
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} tries to update contact with ID {ContactId} is not found in database", LoggedInEmployee.Id); _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); 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); _context.Contacts.Update(newContact);
await _context.SaveChangesAsync();
List<ContactPhone> phones = await _context.ContactsPhones.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync(); List<ContactPhone> phones = await _context.ContactsPhones.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync();
var phoneIds = phones.Select(p => p.Id).ToList(); var phoneIds = phones.Select(p => p.Id).ToList();
@ -505,7 +506,8 @@ namespace Marco.Pms.Services.Helpers
_context.ContactProjectMappings.Add(new ContactProjectMapping _context.ContactProjectMappings.Add(new ContactProjectMapping
{ {
ProjectId = ProjectId, ProjectId = ProjectId,
ContactId = contact.Id ContactId = contact.Id,
TenantId = tenantId
}); });
} }
} }
@ -572,7 +574,19 @@ namespace Marco.Pms.Services.Helpers
UpdateAt = DateTime.UtcNow UpdateAt = DateTime.UtcNow
}); });
await _context.SaveChangesAsync(); 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(); 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(); phones = await _context.ContactsPhones.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync();