68 lines
2.6 KiB
Plaintext
68 lines
2.6 KiB
Plaintext
package com.healthcare.ohctech.controller;
|
|
|
|
import com.healthcare.ohctech.dto.HabitDto;
|
|
import com.healthcare.ohctech.entity.Habit;
|
|
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
|
|
import com.healthcare.ohctech.service.impl.HabitServiceImpl;
|
|
import com.healthcare.ohctech.util.PaginationUtil;
|
|
import jakarta.validation.Valid;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@RequestMapping("/habits")
|
|
public class HabitController {
|
|
|
|
@Autowired
|
|
private HabitServiceImpl habitServiceImpl;
|
|
|
|
@Autowired
|
|
private AuthServiceImpl authServiceImpl;
|
|
|
|
@GetMapping("/{id}")
|
|
public ResponseEntity<?> getHabitById(@PathVariable Long id) {
|
|
Habit habit = habitServiceImpl.getHabitById(id);
|
|
if (habit != null) {
|
|
return new ResponseEntity<>(habit, HttpStatus.OK);
|
|
}
|
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
}
|
|
|
|
@GetMapping
|
|
public ResponseEntity<?> getAllHabits(@RequestParam(required = false) Integer page,
|
|
@RequestParam(required = false) Integer size,
|
|
@RequestParam(required = false) String sortBy,
|
|
@RequestParam(required = false) String sortOrder) {
|
|
Pageable pageable = PaginationUtil.getPageableWithDefaults(page, size, sortBy, sortOrder);
|
|
Page<Habit> habitPage = habitServiceImpl.getAllHabits(pageable);
|
|
Map<String, Object> response = PaginationUtil.getPageResponse(habitPage);
|
|
return new ResponseEntity<>(response, HttpStatus.OK);
|
|
}
|
|
|
|
@PostMapping
|
|
public ResponseEntity<?> addHabit(@Valid @RequestBody HabitDto habitDto) {
|
|
Long userId = authServiceImpl.getCurrentUserId();
|
|
habitServiceImpl.addHabit(habitDto,userId);
|
|
return new ResponseEntity<>("Saved Successfully", HttpStatus.CREATED);
|
|
}
|
|
|
|
@PutMapping("/{id}")
|
|
public ResponseEntity<?> updateHabit(@Valid @RequestBody HabitDto habitDto) {
|
|
Long userId = authServiceImpl.getCurrentUserId();
|
|
habitServiceImpl.updateHabit(habitDto,userId);
|
|
return new ResponseEntity<>("Updated Successfully", HttpStatus.OK);
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
public ResponseEntity<?> deleteHabit(@PathVariable Long id) {
|
|
habitServiceImpl.deleteHabit(id);
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
}
|
|
}
|