Files
jhipster-question-bank/src/main/java/com/example/questionbank/web/rest/ChoiceResource.java
T

172 lines
7.2 KiB
Java

package com.example.questionbank.web.rest;
import com.example.questionbank.repository.ChoiceRepository;
import com.example.questionbank.service.ChoiceService;
import com.example.questionbank.service.dto.ChoiceDTO;
import com.example.questionbank.web.rest.errors.BadRequestAlertException;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.ResponseUtil;
/**
* REST controller for managing {@link com.example.questionbank.domain.Choice}.
*/
@RestController
@RequestMapping("/api/choices")
public class ChoiceResource {
private static final Logger LOG = LoggerFactory.getLogger(ChoiceResource.class);
private static final String ENTITY_NAME = "choice";
@Value("${jhipster.clientApp.name:questionBank}")
private String applicationName;
private final ChoiceService choiceService;
private final ChoiceRepository choiceRepository;
public ChoiceResource(ChoiceService choiceService, ChoiceRepository choiceRepository) {
this.choiceService = choiceService;
this.choiceRepository = choiceRepository;
}
/**
* {@code POST /choices} : Create a new choice.
*
* @param choiceDTO the choiceDTO to create.
* @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new choiceDTO, or with status {@code 400 (Bad Request)} if the choice has already an ID.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PostMapping("")
public ResponseEntity<ChoiceDTO> createChoice(@Valid @RequestBody ChoiceDTO choiceDTO) throws URISyntaxException {
LOG.debug("REST request to save Choice : {}", choiceDTO);
if (choiceDTO.getId() != null) {
throw new BadRequestAlertException("A new choice cannot already have an ID", ENTITY_NAME, "idexists");
}
choiceDTO = choiceService.save(choiceDTO);
return ResponseEntity.created(new URI("/api/choices/" + choiceDTO.getId()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, choiceDTO.getId().toString()))
.body(choiceDTO);
}
/**
* {@code PUT /choices/:id} : Updates an existing choice.
*
* @param id the id of the choiceDTO to save.
* @param choiceDTO the choiceDTO to update.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated choiceDTO,
* or with status {@code 400 (Bad Request)} if the choiceDTO is not valid,
* or with status {@code 500 (Internal Server Error)} if the choiceDTO couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PutMapping("/{id}")
public ResponseEntity<ChoiceDTO> updateChoice(
@PathVariable(value = "id", required = false) final Long id,
@Valid @RequestBody ChoiceDTO choiceDTO
) throws URISyntaxException {
LOG.debug("REST request to update Choice : {}, {}", id, choiceDTO);
if (choiceDTO.getId() == null) {
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
}
if (!Objects.equals(id, choiceDTO.getId())) {
throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
}
if (!choiceRepository.existsById(id)) {
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
}
choiceDTO = choiceService.update(choiceDTO);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, choiceDTO.getId().toString()))
.body(choiceDTO);
}
/**
* {@code PATCH /choices/:id} : Partial updates given fields of an existing choice, field will ignore if it is null
*
* @param id the id of the choiceDTO to save.
* @param choiceDTO the choiceDTO to update.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated choiceDTO,
* or with status {@code 400 (Bad Request)} if the choiceDTO is not valid,
* or with status {@code 404 (Not Found)} if the choiceDTO is not found,
* or with status {@code 500 (Internal Server Error)} if the choiceDTO couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PatchMapping(value = "/{id}", consumes = { "application/json", "application/merge-patch+json" })
public ResponseEntity<ChoiceDTO> partialUpdateChoice(
@PathVariable(value = "id", required = false) final Long id,
@NotNull @RequestBody ChoiceDTO choiceDTO
) throws URISyntaxException {
LOG.debug("REST request to partial update Choice partially : {}, {}", id, choiceDTO);
if (choiceDTO.getId() == null) {
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
}
if (!Objects.equals(id, choiceDTO.getId())) {
throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
}
if (!choiceRepository.existsById(id)) {
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
}
Optional<ChoiceDTO> result = choiceService.partialUpdate(choiceDTO);
return ResponseUtil.wrapOrNotFound(
result,
HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, choiceDTO.getId().toString())
);
}
/**
* {@code GET /choices} : get all the Choices.
*
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of Choices in body.
*/
@GetMapping("")
public List<ChoiceDTO> getAllChoices() {
LOG.debug("REST request to get all Choices");
return choiceService.findAll();
}
/**
* {@code GET /choices/:id} : get the "id" choice.
*
* @param id the id of the choiceDTO to retrieve.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the choiceDTO, or with status {@code 404 (Not Found)}.
*/
@GetMapping("/{id}")
public ResponseEntity<ChoiceDTO> getChoice(@PathVariable("id") Long id) {
LOG.debug("REST request to get Choice : {}", id);
Optional<ChoiceDTO> choiceDTO = choiceService.findOne(id);
return ResponseUtil.wrapOrNotFound(choiceDTO);
}
/**
* {@code DELETE /choices/:id} : delete the "id" choice.
*
* @param id the id of the choiceDTO to delete.
* @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
*/
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteChoice(@PathVariable("id") Long id) {
LOG.debug("REST request to delete Choice : {}", id);
choiceService.delete(id);
return ResponseEntity.noContent()
.headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()))
.build();
}
}