Skip to content

Commit

Permalink
Merge pull request #33 from minkhantthu-developer/main
Browse files Browse the repository at this point in the history
[Complete] Update Agent Model and Add Appointment Update
  • Loading branch information
minkhantthu-developer authored Aug 1, 2024
2 parents 439bc56 + e13b01e commit 9aaf35c
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 21 deletions.
16 changes: 16 additions & 0 deletions REMS.BackendApi/Features/Appointment/AppointmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,21 @@ public async Task<IActionResult> GetAppointmentByAgentId(int id,int pageNo,int p
return StatusCode(StatusCodes.Status500InternalServerError, ex.ToString());
}
}

[HttpPatch("{id}")]
public async Task<IActionResult> 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);
}
}
}
}
4 changes: 3 additions & 1 deletion REMS.Mapper/ChangeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ 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
{
AgentId = agent.AgentId,
UserId = agent.UserId,
AgencyName = agent.AgencyName,
LicenseNumber = agent.LicenseNumber,
Email=user.Email,
PhoneNumber=user.Phone,
Address = agent.Address
};
}
Expand Down
4 changes: 2 additions & 2 deletions REMS.Models/Agent/AgentDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
2 changes: 1 addition & 1 deletion REMS.Modules/Features/Agent/BL_Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<Result<object>> DeleteAgentAsync(int id)

public async Task<Result<AgentDto>> SearchAgentAsync(int id)
{
return await _daAgent.SearchAgentAsync(id);
return await _daAgent.SearchAgentByUserIdAsync(id);
}

public async Task<Result<string>> LoginAgentAsync(AgentLoginRequestModel agentLoginInfo)
Expand Down
26 changes: 19 additions & 7 deletions REMS.Modules/Features/Agent/DA_Agent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace REMS.Modules.Features.Agent;
using REMS.Models.User;

namespace REMS.Modules.Features.Agent;

public class DA_Agent
{
Expand Down Expand Up @@ -35,7 +37,7 @@ public async Task<Result<AgentResponseModel>> CreateAgentAsync(AgentRequestModel
}
var agentResponse = new AgentResponseModel
{
Agent = agent.ChangeAgent()
Agent = agent.ChangeAgent(user)
};
response = Result<AgentResponseModel>.Success(agentResponse, "Agent Register Successfully");
}
Expand Down Expand Up @@ -96,9 +98,9 @@ public async Task<Result<AgentResponseModel>> UpdateAgentAsync(int id, AgentRequ
{
return Result<AgentResponseModel>.Error("Updating Fail");
}
var agentResponse = new AgentResponseModel
AgentResponseModel agentResponse = new AgentResponseModel
{
Agent = agent.ChangeAgent()
Agent = agent.ChangeAgent(user)
};
response = Result<AgentResponseModel>.Success(agentResponse, "Successfully Update");
}
Expand Down Expand Up @@ -167,13 +169,13 @@ public async Task<Result<string>> LoginAgentAsync(AgentLoginRequestModel agentLo
return model;
}

public async Task<Result<AgentDto>> SearchAgentAsync(int id)
public async Task<Result<AgentDto>> SearchAgentByUserIdAsync(int id)
{
Result<AgentDto> 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,
Expand All @@ -183,11 +185,21 @@ public async Task<Result<AgentDto>> 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<AgentDto>.Error("User Not Found");
goto result;
}
agent.PhoneNumber=user.Phone;
agent.Email=user.Email;
model = Result<AgentDto>.Success(agent);
}
catch (Exception ex)
Expand Down
5 changes: 5 additions & 0 deletions REMS.Modules/Features/Appointment/BL_Appointment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public async Task<Result<AppointmentListResponseModel>> GetAppointmentByAgentIdA
return await _daAppointment.GetAppointmentByClientIdAsync(id, pageNo, pageSize);
}

public async Task<Result<AppointmentResponseModel>> UpdateAppointmentAsync(int id, AppointmentRequestModel requestModel)
{
return await _daAppointment.UpdateAppointmentAsync(id, requestModel);
}

private Result<AppointmentResponseModel> CheckAppointmentValue(AppointmentRequestModel requestModel)
{
TimeSpan time;
Expand Down
69 changes: 59 additions & 10 deletions REMS.Modules/Features/Appointment/DA_Appointment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task<Result<AppointmentResponseModel>> CreateAppointmentAsync(Appoi
}
catch (Exception ex)
{
response= Result<AppointmentResponseModel>.Error(ex);
response = Result<AppointmentResponseModel>.Error(ex);
}
return response;
}
Expand All @@ -63,7 +63,7 @@ public async Task<Result<object>> DeleteAppointmentAsync(int id)
.AsNoTracking()
.FirstOrDefaultAsync(x => x.AppointmentId == id);
if (appointment is null)
return Result<object>.Error( "Appointment Not Found.");
return Result<object>.Error("Appointment Not Found.");
_db.Appointments.Remove(appointment);
_db.Entry(appointment).State = EntityState.Deleted;
int result = await _db.SaveChangesAsync();
Expand All @@ -73,7 +73,7 @@ public async Task<Result<object>> DeleteAppointmentAsync(int id)
}
catch (Exception ex)
{
response= Result<object>.Error( ex);
response = Result<object>.Error(ex);
}
return response;
}
Expand All @@ -86,18 +86,18 @@ public async Task<Result<AppointmentListResponseModel>> 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<AppointmentListResponseModel>.Error("No Data Found.");
}
Expand All @@ -120,5 +120,54 @@ public async Task<Result<AppointmentListResponseModel>> GetAppointmentByClientId
}
return response;
}

public async Task<Result<AppointmentResponseModel>> UpdateAppointmentAsync(int id, AppointmentRequestModel requestModel)
{
Result<AppointmentResponseModel> response = null;
try
{
if (requestModel is null)
return Result<AppointmentResponseModel>.Error("Request Model is null");

var appointment = await _db.Appointments
.AsNoTracking()
.FirstOrDefaultAsync(x => x.PropertyId == id); ;
if (appointment is null)
return Result<AppointmentResponseModel>.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<AppointmentResponseModel>.Error("Fail to update appointment");
}
var appointmentResponse = new AppointmentResponseModel
{
Appointment = appointment.Change()
};
response=Result<AppointmentResponseModel>.Success(appointmentResponse,"Appointment Update Successfully");
}
catch (Exception ex)
{
response = Result<AppointmentResponseModel>.Error(ex);
}
return response;
}
}
}

0 comments on commit 9aaf35c

Please sign in to comment.