1
\$\begingroup\$

I would like to do a refactoring of web API implemented in .NET Core. The Goal is to simplify Get method and to implement this to be more according to REST philosophy.

I would appreciate any advice. As you can see the logic has many invalid combinations and too much code in one method.

 namespace Api.Treatments.V2
        {
            public class TreatmentsController : AbstractController
            {
                private readonly ITreatmentsService _treatmentsService;
                private readonly ICourseService _courseService;
                private readonly ICustomersService _customersService;

                public TreatmentsController(
                    IMapper mapper,
                    IConfiguration configuration,
                    ISessionsService sessionsService,
                    ITreatmentsService treatmentsService,
                    ITreatmentTypesService treatmentTypesService)
                    ITreatmentTypesService treatmentTypesService,
                    ICourseService courseService,
                    ICustomersService customersService)
                    : base(mapper, configuration, sessionsService)
                {
                    _treatmentsService = treatmentsService;
                    _courseService = courseService;
                    _customersService = customersService;
                }

                [HttpGet("history")]
                [AuthorizationFilter(Constants.Role.Client, Constants.Role.Artist)]
                public async Task<IActionResult> GetAllTreatmentsAsync([FromQuery] bool myTreatments, [FromQuery] int? clientId, [FromQuery] bool isQuickUser)
                {
                    var session = await GetSession();
                    // prevent invalid value combinations
                    if (myTreatments && (clientId.HasValue || isQuickUser))
                        return BadRequest("In case of my treatments, other parameters must have default values");
                    if (!myTreatments && !clientId.HasValue)
                        return BadRequest("Your client id must be specified");
                    if (!myTreatments && session.Role.Name == Constants.Role.Client)
                        return BadRequest("Client can get only his treatments");

                    if (myTreatments)
                        clientId = (int?)session.UserId;
                    else
                        await _customersService.CheckIfArtistHasAccessToClientAsync(session.LegacyUsername, clientId.Value);

                    // get treatments
                    var treatments = isQuickUser ? await _treatmentsService.GetQuickUserTreatmentsAsync(session.UserId, clientId.Value) :
                                                   await _treatmentsService.GetClientTreatmentsAsync(clientId.Value);
                    var resultTreatments = Mapper.Map<List<TreatmentsResponseDto>>(treatments);
                    // get estimations
                    var estimations = await _courseService.GetCourseResultsAsync(clientId.Value, isQuickUser, 1); // to do: estimation id should be forwarded in better way
                    // map
                    var responseTreatments = Mapper.Map<List<TreatmentsHistoryResponseDto>>(resultTreatments);
                    responseTreatments.ForEach(x => x.TreatmentHistoryType = TreatmentHistoryType.Treatment);
                    var responseEstimations = Mapper.Map<List<TreatmentsHistoryResponseDto>>(estimations);
                    responseEstimations.ForEach(x => x.TreatmentHistoryType = TreatmentHistoryType.Estimation);
                    // merge
                    var response = new List<TreatmentsHistoryResponseDto>();
                    response.AddRange(responseTreatments);
                    response.AddRange(responseEstimations);
                    // sort
                    response = response.OrderByDescending(x => x.Date).ToList();
                    return Ok(response);
                }        
            }
        }
\$\endgroup\$
  • \$\begingroup\$ Can this be separated in multiple methods, or are there some restrictions from client end? \$\endgroup\$ – Hekku2 Dec 16 '19 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.