content: package lk.ijse.gdse.aad68.notetaker.controller;
  
  import lk.ijse.gdse.aad68.notetaker.customObj.NoteResponse;
  import lk.ijse.gdse.aad68.notetaker.customObj.UserResponse;
  import lk.ijse.gdse.aad68.notetaker.exception.DataPersistFailedException;
  import lk.ijse.gdse.aad68.notetaker.exception.NoteNotFoundException;
  import lk.ijse.gdse.aad68.notetaker.service.NoteService;
  import lk.ijse.gdse.aad68.notetaker.dto.impl.NoteDTO;
  import lombok.RequiredArgsConstructor;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.MediaType;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.*;
  
  import java.util.List;
  
  @RestController
  @RequestMapping("api/v1/note")
  @RequiredArgsConstructor
  public class NoteController {
      @Autowired
      private final NoteService noteService;
  
  //    @GetMapping("/healthcheck")
  //    public String healthCheck(){
  //        return "Note Taker is running!";
  //    }
  
      @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
      public ResponseEntity<Void> createNote(@RequestBody NoteDTO note) {
          if (note == null) {
              return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
          }else {
              try{
                  noteService.saveNote(note);
                  return new ResponseEntity<>(HttpStatus.CREATED);
              }catch (DataPersistFailedException e) {
                  return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
              }catch (Exception e) {
                  throw e;
                  //return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
              }
          }
  
      }
  
      @GetMapping(value = "allnotes", produces = MediaType.APPLICATION_JSON_VALUE)
      public List<NoteDTO> getAllNotes(){
          return noteService.getAllNotes();
      }
  
  
      @GetMapping(value = "/{noteId}", produces = MediaType.APPLICATION_JSON_VALUE)
      public NoteResponse getNote(@PathVariable ("noteId") String noteId)  {
          return noteService.getSelectedNote(noteId);
      }
  
  
      @ResponseStatus(HttpStatus.NO_CONTENT)
      @PatchMapping(value = "/{noteId}",produces = MediaType.APPLICATION_JSON_VALUE)
      public ResponseEntity<Void> updateNote(@PathVariable ("noteId") String noteId, @RequestBody NoteDTO note) {
          try{
              noteService.updateNote(noteId, note);
              return new ResponseEntity<>(HttpStatus.NO_CONTENT);
          }catch (NoteNotFoundException e){
              return new ResponseEntity<>(HttpStatus.NOT_FOUND);
          }catch (Exception e){
              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
          }
      }
  
  
      @DeleteMapping(value ="/{noteId}" )
      public ResponseEntity<Void> deleteNote(@PathVariable ("noteId") String noteId) {
          try{
              noteService.deleteNote(noteId);
              return new ResponseEntity<>(HttpStatus.NO_CONTENT);
          }catch (NoteNotFoundException e) {
              return new ResponseEntity<>(HttpStatus.NOT_FOUND);
          }catch (Exception e) {
              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
          }
      }
  }
  
  
  