diff --git a/REMS.BackendApi/Features/Appointment/AppointmentController.cs b/REMS.BackendApi/Features/Appointment/AppointmentController.cs index 00d949d..aabd532 100644 --- a/REMS.BackendApi/Features/Appointment/AppointmentController.cs +++ b/REMS.BackendApi/Features/Appointment/AppointmentController.cs @@ -60,5 +60,21 @@ public async Task GetAppointmentByAgentId(int id,int pageNo,int p return StatusCode(StatusCodes.Status500InternalServerError, ex.ToString()); } } + + [HttpPatch("{id}")] + public async Task UpdateAppointment(int id,AppointmentRequestModel requestModel) + { + try + { + var response = await _blAppointment.UpdateAppointmentAsync(id, requestModel); + if (response.IsError) + return BadRequest(response); + return Ok(response); + } + catch(Exception ex) + { + return StatusCode(StatusCodes.Status500InternalServerError, ex); + } + } } } diff --git a/REMS.Mapper/ChangeModel.cs b/REMS.Mapper/ChangeModel.cs index 628d693..7272cbe 100644 --- a/REMS.Mapper/ChangeModel.cs +++ b/REMS.Mapper/ChangeModel.cs @@ -33,7 +33,7 @@ public static Agent ChangeAgent(this AgentRequestModel requestModel) return agent; } - public static AgentDto ChangeAgent(this Agent agent) + public static AgentDto ChangeAgent(this Agent agent,User user) { return new AgentDto { @@ -41,6 +41,8 @@ public static AgentDto ChangeAgent(this Agent agent) UserId = agent.UserId, AgencyName = agent.AgencyName, LicenseNumber = agent.LicenseNumber, + Email=user.Email, + PhoneNumber=user.Phone, Address = agent.Address }; } @@ -98,6 +100,7 @@ public static PropertyModel Change(this Property dataModel) var propertyModel = new PropertyModel() { PropertyId = dataModel.PropertyId, + AgentId = dataModel.AgentId, Address = dataModel.Address, City = dataModel.City, State = dataModel.State, @@ -109,7 +112,12 @@ public static PropertyModel Change(this Property dataModel) NumberOfBathrooms = dataModel.NumberOfBathrooms, YearBuilt = dataModel.YearBuilt, Description = dataModel.Description, - Status = dataModel.Status + Status = dataModel.Status, + AvailiablityType =dataModel.AvailiablityType, + MinrentalPeriod = dataModel.MinrentalPeriod, + Approvedby = dataModel.Approvedby, + Adddate = dataModel.Adddate, + Editdate = dataModel.Editdate, }; return propertyModel; @@ -117,9 +125,9 @@ public static PropertyModel Change(this Property dataModel) public static Property Change(this PropertyRequestModel requestModel) { - Property property = new Property + Property property = new() { - PropertyId = requestModel.PropertyId ?? 0, + AgentId = requestModel.AgentId, Address = requestModel.Address, City = requestModel.City, State = requestModel.State, @@ -131,8 +139,12 @@ public static Property Change(this PropertyRequestModel requestModel) NumberOfBathrooms = requestModel.NumberOfBathrooms, YearBuilt = requestModel.YearBuilt, Description = requestModel.Description, - Status = requestModel.Status + Status = requestModel.Status, + AvailiablityType = requestModel.AvailiablityType, + MinrentalPeriod = requestModel.MinRentalPeriod, + Approvedby = requestModel.ApprovedBy, }; + return property; } diff --git a/REMS.Models/Agent/AgentDto.cs b/REMS.Models/Agent/AgentDto.cs index e1247db..8d69428 100644 --- a/REMS.Models/Agent/AgentDto.cs +++ b/REMS.Models/Agent/AgentDto.cs @@ -16,9 +16,9 @@ public class AgentDto public string? LicenseNumber { get; set; } = null!; - public string? Phone { get; set; } - public string? Email { get; set; } + public string? PhoneNumber { get; set; } + public string? Address { get; set; } } \ No newline at end of file diff --git a/REMS.Models/Property/PropertyModel.cs b/REMS.Models/Property/PropertyModel.cs index 7ebef10..933c294 100644 --- a/REMS.Models/Property/PropertyModel.cs +++ b/REMS.Models/Property/PropertyModel.cs @@ -4,15 +4,17 @@ public class PropertyModel { public int PropertyId { get; set; } - public string Address { get; set; } + public int? AgentId { get; set; } - public string City { get; set; } + public string Address { get; set; } = null!; - public string State { get; set; } + public string City { get; set; } = null!; - public string ZipCode { get; set; } + public string State { get; set; } = null!; - public string PropertyType { get; set; } + public string ZipCode { get; set; } = null!; + + public string PropertyType { get; set; } = null!; public decimal Price { get; set; } @@ -26,7 +28,15 @@ public class PropertyModel public string? Description { get; set; } - public string Status { get; set; } + public string Status { get; set; } = null!; + + public string AvailiablityType { get; set; } = null!; + + public int? MinrentalPeriod { get; set; } + + public string? Approvedby { get; set; } + + public DateTime? Adddate { get; set; } - public DateTime? DateListed { get; set; } + public DateTime? Editdate { get; set; } } \ No newline at end of file diff --git a/REMS.Models/Property/PropertyRequestModel.cs b/REMS.Models/Property/PropertyRequestModel.cs index b55e198..5dffbe5 100644 --- a/REMS.Models/Property/PropertyRequestModel.cs +++ b/REMS.Models/Property/PropertyRequestModel.cs @@ -1,9 +1,8 @@ -using System; -namespace REMS.Models.Property; +namespace REMS.Models.Property; public class PropertyRequestModel { - public int? PropertyId { get; set; } + public int? AgentId { get; set; } public string Address { get; set; } @@ -29,9 +28,13 @@ public class PropertyRequestModel public string Status { get; set; } - public DateTime? DateListed { get; set; } + public string AvailiablityType { get; set; } - public List Images { get; set; } + public int? MinRentalPeriod { get; set; } + + public string? ApprovedBy { get; set; } + + public List Images { get; set; } = new List(); } public class PropertyImageRequestModel @@ -39,4 +42,3 @@ public class PropertyImageRequestModel public string? ImgBase64 { get; set; } public string? Description { get; set; } } - diff --git a/REMS.Modules/Features/Agent/BL_Agent.cs b/REMS.Modules/Features/Agent/BL_Agent.cs index 1ee067b..f6e290c 100644 --- a/REMS.Modules/Features/Agent/BL_Agent.cs +++ b/REMS.Modules/Features/Agent/BL_Agent.cs @@ -29,7 +29,7 @@ public async Task> DeleteAgentAsync(int id) public async Task> SearchAgentAsync(int id) { - return await _daAgent.SearchAgentAsync(id); + return await _daAgent.SearchAgentByUserIdAsync(id); } public async Task> LoginAgentAsync(AgentLoginRequestModel agentLoginInfo) diff --git a/REMS.Modules/Features/Agent/DA_Agent.cs b/REMS.Modules/Features/Agent/DA_Agent.cs index 255fe80..1c32a08 100644 --- a/REMS.Modules/Features/Agent/DA_Agent.cs +++ b/REMS.Modules/Features/Agent/DA_Agent.cs @@ -1,4 +1,6 @@ -namespace REMS.Modules.Features.Agent; +using REMS.Models.User; + +namespace REMS.Modules.Features.Agent; public class DA_Agent { @@ -35,7 +37,7 @@ public async Task> CreateAgentAsync(AgentRequestModel } var agentResponse = new AgentResponseModel { - Agent = agent.ChangeAgent() + Agent = agent.ChangeAgent(user) }; response = Result.Success(agentResponse, "Agent Register Successfully"); } @@ -96,9 +98,9 @@ public async Task> UpdateAgentAsync(int id, AgentRequ { return Result.Error("Updating Fail"); } - var agentResponse = new AgentResponseModel + AgentResponseModel agentResponse = new AgentResponseModel { - Agent = agent.ChangeAgent() + Agent = agent.ChangeAgent(user) }; response = Result.Success(agentResponse, "Successfully Update"); } @@ -167,13 +169,13 @@ public async Task> LoginAgentAsync(AgentLoginRequestModel agentLo return model; } - public async Task> SearchAgentAsync(int id) + public async Task> SearchAgentByUserIdAsync(int id) { Result model = null; try { AgentDto? agent = await _db.Agents - .Where(ag => ag.AgentId == id) + .Where(ag => ag.UserId == id) .Select(ag => new AgentDto { AgentId = ag.AgentId, @@ -183,11 +185,21 @@ public async Task> SearchAgentAsync(int id) Address = ag.Address }) .FirstOrDefaultAsync(); - if (agent is null) + var user = await _db.Users + .Where(x => x.UserId == id) + .Select(n => new UserModel + { + Phone = n.Phone, + Email = n.Email + }) + .FirstOrDefaultAsync(); + if (agent is null || user is null) { model = Result.Error("User Not Found"); goto result; } + agent.PhoneNumber=user.Phone; + agent.Email=user.Email; model = Result.Success(agent); } catch (Exception ex) diff --git a/REMS.Modules/Features/Appointment/BL_Appointment.cs b/REMS.Modules/Features/Appointment/BL_Appointment.cs index d4c5309..668335c 100644 --- a/REMS.Modules/Features/Appointment/BL_Appointment.cs +++ b/REMS.Modules/Features/Appointment/BL_Appointment.cs @@ -43,6 +43,11 @@ public async Task> GetAppointmentByAgentIdA return await _daAppointment.GetAppointmentByClientIdAsync(id, pageNo, pageSize); } + public async Task> UpdateAppointmentAsync(int id, AppointmentRequestModel requestModel) + { + return await _daAppointment.UpdateAppointmentAsync(id, requestModel); + } + private Result CheckAppointmentValue(AppointmentRequestModel requestModel) { TimeSpan time; diff --git a/REMS.Modules/Features/Appointment/DA_Appointment.cs b/REMS.Modules/Features/Appointment/DA_Appointment.cs index fafa243..80dcb74 100644 --- a/REMS.Modules/Features/Appointment/DA_Appointment.cs +++ b/REMS.Modules/Features/Appointment/DA_Appointment.cs @@ -49,7 +49,7 @@ public async Task> CreateAppointmentAsync(Appoi } catch (Exception ex) { - response= Result.Error(ex); + response = Result.Error(ex); } return response; } @@ -63,7 +63,7 @@ public async Task> DeleteAppointmentAsync(int id) .AsNoTracking() .FirstOrDefaultAsync(x => x.AppointmentId == id); if (appointment is null) - return Result.Error( "Appointment Not Found."); + return Result.Error("Appointment Not Found."); _db.Appointments.Remove(appointment); _db.Entry(appointment).State = EntityState.Deleted; int result = await _db.SaveChangesAsync(); @@ -73,7 +73,7 @@ public async Task> DeleteAppointmentAsync(int id) } catch (Exception ex) { - response= Result.Error( ex); + response = Result.Error(ex); } return response; } @@ -86,18 +86,18 @@ public async Task> GetAppointmentByClientId var query = _db.Appointments .AsNoTracking() .Where(x => x.ClientId == id) - .Select(n=>new AppointmentModel + .Select(n => new AppointmentModel { AppointmentId = n.AppointmentId, - ClientId=n.ClientId, - PropertyId=n.PropertyId, + ClientId = n.ClientId, + PropertyId = n.PropertyId, AppointmentDate = n.AppointmentDate, - AppointmentTime=n.AppointmentTime.ToString(), - Status=n.Status, - Notes=n.Notes + AppointmentTime = n.AppointmentTime.ToString(), + Status = n.Status, + Notes = n.Notes }); var appointmentList = await query.Pagination(pageNo, pageSize).ToListAsync(); - if(appointmentList is null || appointmentList.Count == 0) + if (appointmentList is null || appointmentList.Count == 0) { return Result.Error("No Data Found."); } @@ -120,5 +120,54 @@ public async Task> GetAppointmentByClientId } return response; } + + public async Task> UpdateAppointmentAsync(int id, AppointmentRequestModel requestModel) + { + Result response = null; + try + { + if (requestModel is null) + return Result.Error("Request Model is null"); + + var appointment = await _db.Appointments + .AsNoTracking() + .FirstOrDefaultAsync(x => x.PropertyId == id); ; + if (appointment is null) + return Result.Error("appointment not found"); + + if (!string.IsNullOrWhiteSpace(requestModel.AppointmentDate.ToString())) + { + appointment.AppointmentDate = requestModel.AppointmentDate; + } + if (!string.IsNullOrWhiteSpace(requestModel.AppointmentTime)) + { + appointment.AppointmentTime = TimeSpan.Parse(requestModel.AppointmentTime); + } + if (!string.IsNullOrWhiteSpace(requestModel.Status)) + { + appointment.Status = requestModel.Status; + } + if (!string.IsNullOrWhiteSpace(requestModel.Notes)) + { + appointment.Notes = requestModel.Notes; + } + _db.Entry(appointment).State= EntityState.Modified; + int result=await _db.SaveChangesAsync(); + if (result < 0) + { + return Result.Error("Fail to update appointment"); + } + var appointmentResponse = new AppointmentResponseModel + { + Appointment = appointment.Change() + }; + response=Result.Success(appointmentResponse,"Appointment Update Successfully"); + } + catch (Exception ex) + { + response = Result.Error(ex); + } + return response; + } } } diff --git a/REMS.Modules/Features/Property/DA_Property.cs b/REMS.Modules/Features/Property/DA_Property.cs index 85c9737..d246864 100644 --- a/REMS.Modules/Features/Property/DA_Property.cs +++ b/REMS.Modules/Features/Property/DA_Property.cs @@ -183,6 +183,9 @@ public async Task> CreateProperty(PropertyRequestM { throw new ArgumentNullException(nameof(requestModel), "Request model cannot be null"); } + var isAgentExist = _db.Agents.AsNoTracking() + .FirstOrDefault(x => x.AgentId == requestModel.AgentId) + ?? throw new Exception("Agent Id does not exist"); var property = requestModel.Change() ?? throw new Exception("Failed to convert request model to property entity"); @@ -228,6 +231,10 @@ public async Task> UpdateProperty(int propertyId, .FirstOrDefaultAsync(x => x.PropertyId == propertyId) ?? throw new Exception("Property Not Found"); + var isAgentExist = _db.Agents.AsNoTracking() + .FirstOrDefault(x => x.AgentId == requestModel.AgentId) + ?? throw new Exception("Agent Id does not exist"); + foreach (var propertyImage in property.PropertyImages) { RemovePhotoFromFolder(propertyImage.ImageUrl); @@ -247,7 +254,9 @@ public async Task> UpdateProperty(int propertyId, property.YearBuilt = requestModel.YearBuilt; property.Description = requestModel.Description; property.Status = requestModel.Status; - //property.D = requestModel.DateListed; + property.AvailiablityType = requestModel.AvailiablityType; + property.MinrentalPeriod = requestModel.MinRentalPeriod; + property.Editdate = DateTime.Now; foreach (var propertyImage in requestModel.Images) {