Skip to content

Commit

Permalink
Merge pull request #125 from DentallApp/return-id-response
Browse files Browse the repository at this point in the history
Returns JSON response with the ID of the record inserted in the database
  • Loading branch information
MrDave1999 authored Dec 28, 2022
2 parents 1d74b6d + f00095c commit c40fb7e
Show file tree
Hide file tree
Showing 30 changed files with 62 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Features/Appointments/AppointmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<IEnumerable<AppointmentGetByBasicUserDto>> GetAppointmentsByUs
/// </summary>
[AuthorizeByRole(RolesName.Secretary)]
[HttpPost]
public async Task<ActionResult<Response>> Post([FromBody]AppointmentInsertDto appointmentInsertDto)
public async Task<ActionResult<Response<DtoBase>>> Post([FromBody]AppointmentInsertDto appointmentInsertDto)
{
var response = await _appointmentService.CreateAppointmentAsync(appointmentInsertDto);
if (response.Success)
Expand Down
7 changes: 4 additions & 3 deletions src/Features/Appointments/AppointmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ public AppointmentService(IAppointmentRepository appointmentRepository,
_dateTimeProvider = dateTimeProvider;
}

public async Task<Response> CreateAppointmentAsync(AppointmentInsertDto appointmentInsertDto)
public async Task<Response<DtoBase>> CreateAppointmentAsync(AppointmentInsertDto appointmentInsertDto)
{
if (await _appointmentRepository.IsNotAvailableAsync(appointmentInsertDto))
return new Response(DateAndTimeAppointmentIsNotAvailableMessage);
return new Response<DtoBase>(DateAndTimeAppointmentIsNotAvailableMessage);

var appointment = appointmentInsertDto.MapToAppointment();
_appointmentRepository.Insert(appointment);
await _appointmentRepository.SaveAsync();
await _sendingService.SendAppointmentInformationAsync(appointment.Id, appointmentInsertDto);
return new Response
return new Response<DtoBase>
{
Data = new DtoBase { Id = appointment.Id },
Success = true,
Message = CreateResourceMessage
};
Expand Down
2 changes: 1 addition & 1 deletion src/Features/Appointments/IAppointmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface IAppointmentService
{
Task<Response> CreateAppointmentAsync(AppointmentInsertDto appointmentInsertDto);
Task<Response<DtoBase>> CreateAppointmentAsync(AppointmentInsertDto appointmentInsertDto);
Task<Response> UpdateAppointmentAsync(int id, ClaimsPrincipal currentEmployee, AppointmentUpdateDto appointmentUpdateDto);
Task<Response<IEnumerable<AppointmentGetByEmployeeDto>>> GetAppointmentsForEmployeeAsync(ClaimsPrincipal currentEmployee, AppointmentPostDateDto appointmentPostDto);
}
2 changes: 1 addition & 1 deletion src/Features/Chatbot/AppointmentBotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task<Response<IEnumerable<AvailableTimeRangeDto>>> GetAvailableHour
return await availabilityService.GetAvailableHoursAsync(availableTimeRangeDto);
}

public async Task<Response> CreateScheduledAppointmentAsync(AppointmentInsertDto appointment)
public async Task<Response<DtoBase>> CreateScheduledAppointmentAsync(AppointmentInsertDto appointment)
{
using var scope = _serviceProvider.CreateScope();
var appointmentService = scope.ServiceProvider.GetRequiredService<IAppointmentService>();
Expand Down
2 changes: 1 addition & 1 deletion src/Features/Chatbot/IAppointmentBotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IAppointmentBotService
Task<List<AdaptiveChoice>> GetDentalServicesAsync();
Task<List<AdaptiveChoice>> GetDentistsAsync(int officeId, int specialtyId);
Task<Response<IEnumerable<AvailableTimeRangeDto>>> GetAvailableHoursAsync(AvailableTimeRangePostDto availableTimeRangeDto);
Task<Response> CreateScheduledAppointmentAsync(AppointmentInsertDto appointment);
Task<Response<DtoBase>> CreateScheduledAppointmentAsync(AppointmentInsertDto appointment);
Task<SpecificTreatmentRangeToPayDto> GetRangeToPayAsync(int dentalServiceId);
Task<string> GetDentistScheduleAsync(int dentistId);
}
2 changes: 1 addition & 1 deletion src/Features/Dependents/DependentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public DependentController(IDependentService dependentService, IDependentReposit
}

[HttpPost]
public async Task<ActionResult<Response>> Post([FromBody]DependentInsertDto dependentDto)
public async Task<ActionResult<Response<DtoBase>>> Post([FromBody]DependentInsertDto dependentDto)
{
var response = await _dependentService.CreateDependentAsync(User.GetUserId(), dependentDto);
if (response.Success)
Expand Down
5 changes: 3 additions & 2 deletions src/Features/Dependents/DependentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public DependentService(IUnitOfWork unitOfWork)
_unitOfWork = unitOfWork;
}

public async Task<Response> CreateDependentAsync(int userId, DependentInsertDto dependentDto)
public async Task<Response<DtoBase>> CreateDependentAsync(int userId, DependentInsertDto dependentDto)
{
var person = dependentDto.MapToPerson();
_unitOfWork.PersonRepository.Insert(person);
Expand All @@ -19,8 +19,9 @@ public async Task<Response> CreateDependentAsync(int userId, DependentInsertDto
dependent.Person = person;
await _unitOfWork.SaveChangesAsync();

return new Response
return new Response<DtoBase>
{
Data = new DtoBase { Id = dependent.Id },
Success = true,
Message = CreateResourceMessage
};
Expand Down
2 changes: 1 addition & 1 deletion src/Features/Dependents/IDependentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface IDependentService
{
Task<Response> CreateDependentAsync(int userId, DependentInsertDto dependentDto);
Task<Response<DtoBase>> CreateDependentAsync(int userId, DependentInsertDto dependentDto);
Task<Response> RemoveDependentAsync(int dependentId, int userId);
Task<Response> UpdateDependentAsync(int dependentId, int userId, DependentUpdateDto dependentDto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task<IEnumerable<EmployeeScheduleGetDto>> GetByEmployeeId(int emplo
/// Crea un nuevo horario para el empleado.
/// </summary>
[HttpPost]
public async Task<ActionResult<Response>> Post([FromBody]EmployeeScheduleInsertDto employeeScheduleDto)
public async Task<ActionResult<Response<DtoBase>>> Post([FromBody]EmployeeScheduleInsertDto employeeScheduleDto)
{
var response = await _employeeScheduleService.CreateEmployeeScheduleAsync(employeeScheduleDto);
if (response.Success)
Expand Down
5 changes: 3 additions & 2 deletions src/Features/EmployeeSchedules/EmployeeScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ public EmployeeScheduleService(IEmployeeScheduleRepository employeeScheduleRepos
_employeeScheduleRepository = employeeScheduleRepository;
}

public async Task<Response> CreateEmployeeScheduleAsync(EmployeeScheduleInsertDto employeeScheduleDto)
public async Task<Response<DtoBase>> CreateEmployeeScheduleAsync(EmployeeScheduleInsertDto employeeScheduleDto)
{
var employeeSchedule = employeeScheduleDto.MapToEmployeeSchedule();
_employeeScheduleRepository.Insert(employeeSchedule);
await _employeeScheduleRepository.SaveAsync();

return new Response
return new Response<DtoBase>
{
Data = new DtoBase { Id = employeeSchedule.Id },
Success = true,
Message = CreateResourceMessage
};
Expand Down
2 changes: 1 addition & 1 deletion src/Features/EmployeeSchedules/IEmployeeScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public interface IEmployeeScheduleService
{
Task<Response> CreateEmployeeScheduleAsync(EmployeeScheduleInsertDto employeeScheduleDto);
Task<Response<DtoBase>> CreateEmployeeScheduleAsync(EmployeeScheduleInsertDto employeeScheduleDto);
Task<Response> UpdateEmployeeScheduleAsync(int scheduleId, EmployeeScheduleUpdateDto employeeScheduleDto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task<ActionResult<Response<GeneralTreatmentGetDto>>> Get(int id)

[AuthorizeByRole(RolesName.Superadmin)]
[HttpPost]
public async Task<ActionResult<Response>> Post([FromForm]GeneralTreatmentInsertDto treatmentInsertDto)
public async Task<ActionResult<Response<DtoBase>>> Post([FromForm]GeneralTreatmentInsertDto treatmentInsertDto)
{
var response = await _treatmentService.CreateTreatmentAsync(treatmentInsertDto);
if (response.Success)
Expand Down
5 changes: 3 additions & 2 deletions src/Features/GeneralTreatments/GeneralTreatmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ public async Task<Response<GeneralTreatmentGetDto>> GetTreatmentByIdAsync(int id
};
}

public async Task<Response> CreateTreatmentAsync(GeneralTreatmentInsertDto treatmentInsertDto)
public async Task<Response<DtoBase>> CreateTreatmentAsync(GeneralTreatmentInsertDto treatmentInsertDto)
{
var treatment = treatmentInsertDto.MapToGeneralTreatment();
_treatmentRepository.Insert(treatment);
await treatmentInsertDto.Image.WriteAsync(Path.Combine(_basePath, treatment.ImageUrl));
await _treatmentRepository.SaveAsync();

return new Response
return new Response<DtoBase>
{
Data = new DtoBase { Id = treatment.Id },
Success = true,
Message = CreateResourceMessage
};
Expand Down
2 changes: 1 addition & 1 deletion src/Features/GeneralTreatments/IGeneralTreatmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public interface IGeneralTreatmentService
{
Task<Response<GeneralTreatmentGetDto>> GetTreatmentByIdAsync(int id);
Task<Response> CreateTreatmentAsync(GeneralTreatmentInsertDto treatmentInsertDto);
Task<Response<DtoBase>> CreateTreatmentAsync(GeneralTreatmentInsertDto treatmentInsertDto);
Task<Response> UpdateTreatmentAsync(int id, GeneralTreatmentUpdateDto treatmentUpdateDto);
Task<Response> RemoveTreatmentAsync(int id);
}
2 changes: 1 addition & 1 deletion src/Features/OfficeSchedules/IOfficeScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface IOfficeScheduleService
{
Task<Response> CreateOfficeScheduleAsync(ClaimsPrincipal currentEmployee, OfficeScheduleInsertDto officeScheduleInsertDto);
Task<Response<DtoBase>> CreateOfficeScheduleAsync(ClaimsPrincipal currentEmployee, OfficeScheduleInsertDto officeScheduleInsertDto);
Task<Response> UpdateOfficeScheduleAsync(int scheduleId, ClaimsPrincipal currentEmployee, OfficeScheduleUpdateDto officeScheduleUpdateDto);
Task<IEnumerable<OfficeScheduleGetAllDto>> GetAllOfficeSchedulesAsync();
Task<IEnumerable<OfficeScheduleShowDto>> GetHomePageSchedulesAsync();
Expand Down
2 changes: 1 addition & 1 deletion src/Features/OfficeSchedules/OfficeScheduleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<IEnumerable<OfficeScheduleGetDto>> GetByOfficeId(int officeId)
/// </summary>
[AuthorizeByRole(RolesName.Admin, RolesName.Superadmin)]
[HttpPost]
public async Task<ActionResult<Response>> Post([FromBody]OfficeScheduleInsertDto officeScheduleInsertDto)
public async Task<ActionResult<Response<DtoBase>>> Post([FromBody]OfficeScheduleInsertDto officeScheduleInsertDto)
{
var response = await _officeScheduleService.CreateOfficeScheduleAsync(User, officeScheduleInsertDto);
if (response.Success)
Expand Down
10 changes: 6 additions & 4 deletions src/Features/OfficeSchedules/OfficeScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ public OfficeScheduleService(IOfficeScheduleRepository officeScheduleRepository)
_officeScheduleRepository = officeScheduleRepository;
}

public async Task<Response> CreateOfficeScheduleAsync(ClaimsPrincipal currentEmployee, OfficeScheduleInsertDto officeScheduleInsertDto)
public async Task<Response<DtoBase>> CreateOfficeScheduleAsync(ClaimsPrincipal currentEmployee, OfficeScheduleInsertDto officeScheduleInsertDto)
{
if (currentEmployee.IsAdmin() && currentEmployee.IsNotInOffice(officeScheduleInsertDto.OfficeId))
return new Response(OfficeNotAssignedMessage);
return new Response<DtoBase>(OfficeNotAssignedMessage);

_officeScheduleRepository.Insert(officeScheduleInsertDto.MapToOfficeSchedule());
var officeSchedule = officeScheduleInsertDto.MapToOfficeSchedule();
_officeScheduleRepository.Insert(officeSchedule);
await _officeScheduleRepository.SaveAsync();

return new Response
return new Response<DtoBase>
{
Data = new DtoBase { Id = officeSchedule.Id },
Success = true,
Message = CreateResourceMessage
};
Expand Down
2 changes: 1 addition & 1 deletion src/Features/Offices/IOfficeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public interface IOfficeService
{
Task<Response> CreateOfficeAsync(OfficeInsertDto officeInsertDto);
Task<Response<DtoBase>> CreateOfficeAsync(OfficeInsertDto officeInsertDto);
Task<Response> UpdateOfficeAsync(int officeId, int currentEmployeeId, OfficeUpdateDto officeUpdateDto);
}
2 changes: 1 addition & 1 deletion src/Features/Offices/OfficeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task<IEnumerable<OfficeShowDto>> GetOfficesForEdit()
/// </summary>
[AuthorizeByRole(RolesName.Superadmin)]
[HttpPost]
public async Task<ActionResult<Response>> Post([FromBody]OfficeInsertDto officeInsertDto)
public async Task<ActionResult<Response<DtoBase>>> Post([FromBody]OfficeInsertDto officeInsertDto)
{
var response = await _officeService.CreateOfficeAsync(officeInsertDto);
if (response.Success)
Expand Down
8 changes: 5 additions & 3 deletions src/Features/Offices/OfficeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ public OfficeService(IOfficeRepository officeRepository)
_officeRepository = officeRepository;
}

public async Task<Response> CreateOfficeAsync(OfficeInsertDto officeInsertDto)
public async Task<Response<DtoBase>> CreateOfficeAsync(OfficeInsertDto officeInsertDto)
{
_officeRepository.Insert(officeInsertDto.MapToOffice());
var office = officeInsertDto.MapToOffice();
_officeRepository.Insert(office);
await _officeRepository.SaveAsync();

return new Response
return new Response<DtoBase>
{
Data = new DtoBase { Id = office .Id },
Success = true,
Message = CreateResourceMessage
};
Expand Down
2 changes: 1 addition & 1 deletion src/Features/PublicHolidays/IPublicHolidayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface IPublicHolidayService
{
Task<Response> CreatePublicHolidayAsync(PublicHolidayInsertDto holidayInsertDto);
Task<Response<DtoBase>> CreatePublicHolidayAsync(PublicHolidayInsertDto holidayInsertDto);
Task<Response> RemovePublicHolidayAsync(int id);
Task<Response> UpdatePublicHolidayAsync(int id, PublicHolidayUpdateDto holidayUpdateDto);
}
2 changes: 1 addition & 1 deletion src/Features/PublicHolidays/PublicHolidayController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public PublicHolidayController(IPublicHolidayService holidayService,
}

[HttpPost]
public async Task<ActionResult<Response>> Post([FromBody]PublicHolidayInsertDto holidayInsertDto)
public async Task<ActionResult<Response<DtoBase>>> Post([FromBody]PublicHolidayInsertDto holidayInsertDto)
{
var response = await _holidayService.CreatePublicHolidayAsync(holidayInsertDto);
if (response.Success)
Expand Down
5 changes: 3 additions & 2 deletions src/Features/PublicHolidays/PublicHolidayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public PublicHolidayService(IUnitOfWork unitOfWork)
_unitOfWork = unitOfWork;
}

public async Task<Response> CreatePublicHolidayAsync(PublicHolidayInsertDto holidayInsertDto)
public async Task<Response<DtoBase>> CreatePublicHolidayAsync(PublicHolidayInsertDto holidayInsertDto)
{
var publicHoliday = holidayInsertDto.MapToPublicHoliday();
_unitOfWork.PublicHolidayRepository.Insert(publicHoliday);
Expand All @@ -21,8 +21,9 @@ public async Task<Response> CreatePublicHolidayAsync(PublicHolidayInsertDto holi
}
await _unitOfWork.SaveChangesAsync();

return new Response
return new Response<DtoBase>
{
Data = new DtoBase { Id = publicHoliday.Id },
Success = true,
Message = CreateResourceMessage
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface ISpecificTreatmentService
{
Task<Response> CreateSpecificTreatmentAsync(SpecificTreatmentInsertDto treatmentInsertDto);
Task<Response<DtoBase>> CreateSpecificTreatmentAsync(SpecificTreatmentInsertDto treatmentInsertDto);
Task<Response> UpdateSpecificTreatmentAsync(int id, SpecificTreatmentUpdateDto treatmentUpdateDto);
Task<Response> RemoveSpecificTreatmentAsync(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<IEnumerable<SpecificTreatmentShowDto>> Get()

[AuthorizeByRole(RolesName.Superadmin)]
[HttpPost]
public async Task<ActionResult<Response>> Post([FromBody]SpecificTreatmentInsertDto treatmentInsertDto)
public async Task<ActionResult<Response<DtoBase>>> Post([FromBody]SpecificTreatmentInsertDto treatmentInsertDto)
{
var response = await _treatmentService.CreateSpecificTreatmentAsync(treatmentInsertDto);
if (response.Success)
Expand Down
5 changes: 3 additions & 2 deletions src/Features/SpecificTreatments/SpecificTreatmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ public SpecificTreatmentService(ISpecificTreatmentRepository treatmentRepository
_treatmentRepository = treatmentRepository;
}

public async Task<Response> CreateSpecificTreatmentAsync(SpecificTreatmentInsertDto treatmentInsertDto)
public async Task<Response<DtoBase>> CreateSpecificTreatmentAsync(SpecificTreatmentInsertDto treatmentInsertDto)
{
var specificTreatment = treatmentInsertDto.MapToSpecificTreatment();
_treatmentRepository.Insert(specificTreatment);
await _treatmentRepository.SaveAsync();

return new Response
return new Response<DtoBase>
{
Data = new DtoBase { Id = specificTreatment.Id },
Success = true,
Message = CreateResourceMessage
};
Expand Down
2 changes: 1 addition & 1 deletion src/Features/UserRegistration/IUserRegisterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public interface IUserRegisterService
{
Task<Response> CreateBasicUserAccountAsync(UserInsertDto userInsertDto);
Task<Response> CreateEmployeeAccountAsync(ClaimsPrincipal currentEmployee, EmployeeInsertDto employeeInsertDto);
Task<Response<DtoBase>> CreateEmployeeAccountAsync(ClaimsPrincipal currentEmployee, EmployeeInsertDto employeeInsertDto);
}
2 changes: 1 addition & 1 deletion src/Features/UserRegistration/UserRegisterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task<ActionResult<Response>> CreateBasicUserAccount([FromBody]UserI
[AuthorizeByRole(RolesName.Admin, RolesName.Superadmin)]
[Route("employee")]
[HttpPost]
public async Task<ActionResult<Response>> CreateEmployeeAccount([FromBody]EmployeeInsertDto employeeInsertDto)
public async Task<ActionResult<Response<DtoBase>>> CreateEmployeeAccount([FromBody]EmployeeInsertDto employeeInsertDto)
{
var response = await _userRegisterService.CreateEmployeeAccountAsync(User, employeeInsertDto);
if (response.Success)
Expand Down
11 changes: 6 additions & 5 deletions src/Features/UserRegistration/UserRegisterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ public async Task<Response> CreateBasicUserAccountAsync(UserInsertDto userInsert
};
}

public async Task<Response> CreateEmployeeAccountAsync(ClaimsPrincipal currentEmployee, EmployeeInsertDto employeeInsertDto)
public async Task<Response<DtoBase>> CreateEmployeeAccountAsync(ClaimsPrincipal currentEmployee, EmployeeInsertDto employeeInsertDto)
{
if (await _unitOfWork.UserRepository.UserExistsAsync(employeeInsertDto.UserName))
return new Response(UsernameAlreadyExistsMessage);
return new Response<DtoBase>(UsernameAlreadyExistsMessage);

if (currentEmployee.IsAdmin() && currentEmployee.IsNotInOffice(employeeInsertDto.OfficeId))
return new Response(OfficeNotAssignedMessage);
return new Response<DtoBase>(OfficeNotAssignedMessage);

if (currentEmployee.HasNotPermissions(employeeInsertDto.Roles))
return new Response(PermitsNotGrantedMessage);
return new Response<DtoBase>(PermitsNotGrantedMessage);

var user = CreateUserAccount(employeeInsertDto, employeeInsertDto.Roles.RemoveDuplicates());
var employee = employeeInsertDto.MapToEmployee();
Expand All @@ -81,8 +81,9 @@ public async Task<Response> CreateEmployeeAccountAsync(ClaimsPrincipal currentEm
}
await _unitOfWork.SaveChangesAsync();

return new Response
return new Response<DtoBase>
{
Data = new DtoBase { Id = employee.Id },
Success = true,
Message = CreateEmployeeAccountMessage
};
Expand Down
6 changes: 6 additions & 0 deletions src/Responses/DtoBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DentallApp.Responses;

public class DtoBase
{
public int Id { get; set; }
}

0 comments on commit c40fb7e

Please sign in to comment.