From 895f22c62a85a90cc2059cd1fe9d481adb680d56 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Sat, 17 May 2025 12:41:02 +0530 Subject: [PATCH] properly mapped the updated Dto to contact table --- Marco.Pms.Model/Mapper/DirectoryMapper.cs | 4 +++- Marco.Pms.Services/Helpers/DirectoryHelper.cs | 22 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Marco.Pms.Model/Mapper/DirectoryMapper.cs b/Marco.Pms.Model/Mapper/DirectoryMapper.cs index 71a0f6a..f78c260 100644 --- a/Marco.Pms.Model/Mapper/DirectoryMapper.cs +++ b/Marco.Pms.Model/Mapper/DirectoryMapper.cs @@ -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, diff --git a/Marco.Pms.Services/Helpers/DirectoryHelper.cs b/Marco.Pms.Services/Helpers/DirectoryHelper.cs index ff3f730..f1100c6 100644 --- a/Marco.Pms.Services/Helpers/DirectoryHelper.cs +++ b/Marco.Pms.Services/Helpers/DirectoryHelper.cs @@ -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); return ApiResponse.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.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 phones = await _context.ContactsPhones.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync(); var phoneIds = phones.Select(p => p.Id).ToList(); @@ -505,7 +506,8 @@ namespace Marco.Pms.Services.Helpers _context.ContactProjectMappings.Add(new ContactProjectMapping { ProjectId = ProjectId, - ContactId = contact.Id + ContactId = contact.Id, + TenantId = tenantId }); } } @@ -572,7 +574,19 @@ namespace Marco.Pms.Services.Helpers UpdateAt = DateTime.UtcNow }); - await _context.SaveChangesAsync(); + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateException dbEx) + { + + return ApiResponse.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();