|
|
|
|
@ -1,5 +1,6 @@ |
|
|
|
|
package de.kreth.clubhelper.model.controller; |
|
|
|
|
|
|
|
|
|
import java.time.LocalDate; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
@ -16,6 +17,9 @@ import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody; |
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
|
|
|
|
|
|
import de.kreth.clubhelper.model.dao.AttendanceDao; |
|
|
|
|
import de.kreth.clubhelper.model.dao.PersonDao; |
|
|
|
|
import de.kreth.clubhelper.model.data.Attendance; |
|
|
|
|
@ -24,41 +28,44 @@ import de.kreth.clubhelper.model.data.Person; |
|
|
|
|
@Controller |
|
|
|
|
@RequestMapping("/attendance") |
|
|
|
|
@PreAuthorize("hasRole('trainer') or hasRole('admin')") |
|
|
|
|
public class AttendanceController |
|
|
|
|
{ |
|
|
|
|
public class AttendanceController { |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private AttendanceDao attendanceDao; |
|
|
|
|
@Autowired |
|
|
|
|
private AttendanceDao attendanceDao; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private PersonDao personDao; |
|
|
|
|
@Autowired |
|
|
|
|
private PersonDao personDao; |
|
|
|
|
|
|
|
|
|
@GetMapping(value = "/on") |
|
|
|
|
@ResponseBody |
|
|
|
|
public List<Attendance> getAttendencesOn(@RequestBody @DateTimeFormat(iso=ISO.DATE) Date date) |
|
|
|
|
{ |
|
|
|
|
if (date == null) { |
|
|
|
|
date = new Date(); |
|
|
|
|
} |
|
|
|
|
return attendanceDao.findByOnDate(date); |
|
|
|
|
} |
|
|
|
|
@GetMapping(value = "/{date}") |
|
|
|
|
@ResponseBody |
|
|
|
|
public List<Attendance> getAttendencesOn(@PathVariable("date") @DateTimeFormat(iso = ISO.DATE) LocalDate date) { |
|
|
|
|
List<Attendance> findByOnDate = attendanceDao.findByOnDate(date); |
|
|
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
|
|
try { |
|
|
|
|
String one = mapper.writeValueAsString(findByOnDate.get(0)); |
|
|
|
|
System.out.println(one); |
|
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
return findByOnDate; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@PostMapping(value = "/for/{id}") |
|
|
|
|
@ResponseBody |
|
|
|
|
public Attendance post(@PathVariable("id") Long id) |
|
|
|
|
{ |
|
|
|
|
Attendance att = new Attendance(); |
|
|
|
|
att.setOnDate(new Date()); |
|
|
|
|
att.setPerson(personDao.findById(id).orElseThrow(() -> new RuntimeException("Person not found by id=" + id))); |
|
|
|
|
attendanceDao.save(att); |
|
|
|
|
return att; |
|
|
|
|
} |
|
|
|
|
@PostMapping(value = "/for/{id}") |
|
|
|
|
@ResponseBody |
|
|
|
|
public Attendance post(@PathVariable("id") Long id) { |
|
|
|
|
Attendance att = new Attendance(); |
|
|
|
|
att.setOnDate(LocalDate.now()); |
|
|
|
|
att.setPerson(personDao.findById(id).orElseThrow(() -> new RuntimeException("Person not found by id=" + id))); |
|
|
|
|
attendanceDao.save(att); |
|
|
|
|
return att; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@DeleteMapping("/{id}") |
|
|
|
|
public Attendance delete(@PathVariable("id") Long personId, @RequestBody(required = true) Date onDate) { |
|
|
|
|
Person person = personDao.findById(personId).orElseThrow(() -> new RuntimeException("Person not found by id=" + personId)); |
|
|
|
|
Attendance attendance = attendanceDao.findByPersonAndOnDate(person, onDate); |
|
|
|
|
attendanceDao.delete(attendance); |
|
|
|
|
return attendance; |
|
|
|
|
} |
|
|
|
|
@DeleteMapping("/{id}") |
|
|
|
|
public Attendance delete(@PathVariable("id") Long personId, @RequestBody(required = true) Date onDate) { |
|
|
|
|
Person person = personDao.findById(personId) |
|
|
|
|
.orElseThrow(() -> new RuntimeException("Person not found by id=" + personId)); |
|
|
|
|
Attendance attendance = attendanceDao.findByPersonAndOnDate(person, onDate); |
|
|
|
|
attendanceDao.delete(attendance); |
|
|
|
|
return attendance; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|