<commit_before>package uk.gov.ons.ctp.response.casesvc.endpoint;

import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.ons.ctp.common.endpoint.CTPEndpoint;
import uk.gov.ons.ctp.common.error.CTPException;
import uk.gov.ons.ctp.response.casesvc.domain.model.Sample;
import uk.gov.ons.ctp.response.casesvc.representation.GeographyDTO;
import uk.gov.ons.ctp.response.casesvc.representation.SampleDTO;
import uk.gov.ons.ctp.response.casesvc.service.SampleService;

/**
 * Sample endpoint including functionality to create cases for a given sample
 * ID, geography type and geography code
 */
@RestController
@RequestMapping(value = "/samples", consumes = "application/json", produces = "application/json")
@Slf4j
public final class SampleEndpoint implements CTPEndpoint {

  @Autowired
  private SampleService sampleService;

  @Qualifier("caseSvcBeanMapper")
  @Autowired
  private MapperFacade mapperFacade;

  /**
   * the GET endpoint to retrieve a sample by its id
   * @param sampleId the id of the sample
   * @return the sample representation
   * @throws CTPException something went wrong
   */
  @RequestMapping(value = "/{sampleid}", method = RequestMethod.GET)
  public SampleDTO findSampleBySampleId(@PathVariable("sampleid") final Integer sampleId) throws CTPException {
    log.info("Entering findSampleBySampleId with {}", sampleId);
    Sample sample = sampleService.findSampleBySampleId(sampleId);
    if (sample == null) {
      throw new CTPException(CTPException.Fault.RESOURCE_NOT_FOUND, "Sample not found for id %s", sampleId);
    }
    return mapperFacade.map(sample, SampleDTO.class);
  }

  /**
   * the PUT endpoint to create cases
   * @param sampleId the id of the sample
   * @param geography the geography
   * @return the sample representation
   * @throws CTPException something went wrong
   */
  @RequestMapping(value = "/{sampleid}", method = RequestMethod.PUT)
  public SampleDTO createCases(@PathVariable("sampleId") final int sampleId, final GeographyDTO geography)
          throws CTPException {
    log.info("Creating cases ");
    Sample sample = sampleService.findSampleBySampleId(sampleId);
    if (sample == null) {
      throw new CTPException(CTPException.Fault.RESOURCE_NOT_FOUND, "Sample not found for id %s", sampleId);
    }
    sampleService.generateCases(sampleId, geography.getType(), geography.getCode());
    return mapperFacade.map(sample, SampleDTO.class);
  }
}<commit_msg>ctpa-1187 correcting the sample endpoint<commit_after>package uk.gov.ons.ctp.response.casesvc.endpoint;

import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import uk.gov.ons.ctp.common.endpoint.CTPEndpoint;
import uk.gov.ons.ctp.common.error.CTPException;
import uk.gov.ons.ctp.common.error.InvalidRequestException;
import uk.gov.ons.ctp.response.casesvc.domain.model.Sample;
import uk.gov.ons.ctp.response.casesvc.representation.GeographyDTO;
import uk.gov.ons.ctp.response.casesvc.representation.SampleDTO;
import uk.gov.ons.ctp.response.casesvc.service.SampleService;

import javax.validation.Valid;

/**
 * Sample endpoint including functionality to create cases for a given sample
 * ID, geography type and geography code
 */
@RestController
@RequestMapping(value = "/samples", produces = "application/json")
@Slf4j
public final class SampleEndpoint implements CTPEndpoint {

  @Autowired
  private SampleService sampleService;

  @Qualifier("caseSvcBeanMapper")
  @Autowired
  private MapperFacade mapperFacade;

  /**
   * the GET endpoint to retrieve a sample by its id
   * @param sampleId the id of the sample
   * @return the sample representation
   * @throws CTPException something went wrong
   */
  @RequestMapping(value = "/{sampleid}", method = RequestMethod.GET)
  public SampleDTO findSampleBySampleId(@PathVariable("sampleid") final Integer sampleId) throws CTPException {
    log.info("Entering findSampleBySampleId with {}", sampleId);
    Sample sample = sampleService.findSampleBySampleId(sampleId);
    if (sample == null) {
      throw new CTPException(CTPException.Fault.RESOURCE_NOT_FOUND, "Sample not found for id %s", sampleId);
    }
    return mapperFacade.map(sample, SampleDTO.class);
  }

  /**
   * the PUT endpoint to create cases
   * @param sampleId the id of the sample
   * @param geography the geography
   * @return the sample representation
   * @throws CTPException something went wrong
   */
  @RequestMapping(value = "/{sampleid}", method = RequestMethod.PUT, consumes = "application/json")
  public SampleDTO createCases(@PathVariable("sampleId") final int sampleId,
                               @RequestBody @Valid final GeographyDTO geography, BindingResult bindingResult)
          throws CTPException {
    log.info("Creating cases ");
    if (bindingResult.hasErrors()) {
      throw new InvalidRequestException("Binding errors for update action: ", bindingResult);
    }

    Sample sample = sampleService.findSampleBySampleId(sampleId);
    if (sample == null) {
      throw new CTPException(CTPException.Fault.RESOURCE_NOT_FOUND, "Sample not found for id %s", sampleId);
    }
    sampleService.generateCases(sampleId, geography.getType(), geography.getCode());
    return mapperFacade.map(sample, SampleDTO.class);
  }
}
{'fraction_non_alphanumeric': 0.10103935934571477, 'fraction_numerical': 0.0017038677798602829, 'mean_word_length': 4.715676728334956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '143337', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1691, 'n_words': 410}