attendance started

REL-BRANCH-ClubhelperModel-0.0.1
kre6513 5 years ago
parent f667b36d35
commit 8bb9156ac5
  1. 72
      src/main/java/de/kreth/clubhelper/model/controller/AttendanceController.java
  2. 50
      src/main/java/de/kreth/clubhelper/model/controller/GroupContoller.java
  3. 4
      src/main/java/de/kreth/clubhelper/model/controller/PersonController.java
  4. 1
      src/main/java/de/kreth/clubhelper/model/dao/AttendanceDao.java
  5. 239
      src/main/java/de/kreth/clubhelper/model/data/BaseEntity.java
  6. 74
      src/main/java/de/kreth/clubhelper/model/data/GroupDef.java
  7. 114
      src/test/java/de/kreth/clubhelper/model/LoadPersonsTest.java
  8. 94
      src/test/java/de/kreth/clubhelper/model/PersonControllerTest.java

@ -6,6 +6,7 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@ -15,41 +16,46 @@ import org.springframework.web.bind.annotation.ResponseBody;
import de.kreth.clubhelper.model.dao.AttendanceDao;
import de.kreth.clubhelper.model.dao.PersonDao;
import de.kreth.clubhelper.model.data.Attendance;
import de.kreth.clubhelper.model.data.Person;
@Controller
@RequestMapping("/attendance")
@PreAuthorize("hasRole('trainer') or hasRole('admin')")
public class AttendanceController {
private AttendanceDao attendanceDao;
private PersonDao personDao;
@Autowired
public AttendanceController(AttendanceDao attendanceDao, PersonDao personDao) {
this.attendanceDao = attendanceDao;
this.personDao = personDao;
}
@PostMapping(value = "/on")
@ResponseBody
public List<Attendance> getAttendencesOn(@RequestBody(required = false) Date date) {
if (date == null) {
date = new Date();
}
return attendanceDao.findByOnDate(date);
}
@PostMapping(value = "/for/{id}")
@ResponseBody
public Attendance post(@PathVariable("id") Integer 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;
}
public class AttendanceController
{
@Autowired
private AttendanceDao attendanceDao;
@Autowired
private PersonDao personDao;
@PostMapping(value = "/on")
@ResponseBody
public List<Attendance> getAttendencesOn(@RequestBody(required = false) Date date)
{
if (date == null) {
date = new Date();
}
return attendanceDao.findByOnDate(date);
}
@PostMapping(value = "/for/{id}")
@ResponseBody
public Attendance post(@PathVariable("id") Integer 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;
}
@DeleteMapping("/{id}")
public Attendance delete(@PathVariable("id") int 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;
}
}

@ -0,0 +1,50 @@
package de.kreth.clubhelper.model.controller;
import java.nio.charset.Charset;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import de.kreth.clubhelper.model.dao.GroupDao;
import de.kreth.clubhelper.model.data.GroupDef;
@RestController
@RequestMapping("/group")
@PreAuthorize("isAuthenticated()")
public class GroupContoller
{
@Autowired
private GroupDao groupDao;
@GetMapping
public List<GroupDef> allGroups() {
return StreamSupport.stream(groupDao.findAll().spliterator(), false)
.collect(Collectors.toList());
}
@GetMapping("/{id}")
public GroupDef getById(@PathVariable("id") int id) {
Supplier<HttpClientErrorException> supplier = () -> createNotFoundForId(id);
return groupDao.findById(id).orElseThrow(supplier);
}
private HttpClientErrorException createNotFoundForId(Integer id)
{
HttpHeaders headers = new HttpHeaders();
byte[] body = new byte[0];
Charset charset = Charset.forName("UTF-8");
HttpClientErrorException ex = HttpClientErrorException.create(HttpStatus.NOT_FOUND, "Group not found by id=" + id, headers, body, charset);
return ex;
}
}

@ -16,6 +16,7 @@ import de.kreth.clubhelper.model.dao.PersonDao;
import de.kreth.clubhelper.model.data.Adress;
import de.kreth.clubhelper.model.data.Contact;
import de.kreth.clubhelper.model.data.Person;
import io.swagger.annotations.ApiOperation;
@Controller
@RequestMapping("/person")
@ -24,13 +25,16 @@ public class PersonController
{
@Autowired
private PersonDao personDao;
@Autowired
private ContactController contactController;
@Autowired
private AdressController adressController;
@GetMapping
@PreAuthorize("hasAnyRole('trainer', 'admin')")
@ApiOperation("Get a list of all persons. Restricted to trainers and admins.")
public @ResponseBody Iterable<Person> getAll()
{
return personDao.findAll();

@ -14,4 +14,5 @@ public interface AttendanceDao extends CrudRepository<Attendance, Integer> {
List<Attendance> findByPerson(Person person);
Attendance findByPersonAndOnDate(Person person, Date onDate);
}

@ -10,119 +10,128 @@ import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@MappedSuperclass
public abstract class BaseEntity implements EntityAccessor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Temporal(TemporalType.TIMESTAMP)
private Date changed;
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@Temporal(TemporalType.TIMESTAMP)
private Date deleted;
public Date getChanged() {
if (changed == null) {
return null;
}
return new Date(this.changed.getTime());
}
@Override
public void setChanged(Date changed) {
this.changed = new Date(changed.getTime());
}
public Date getCreated() {
if (created == null) {
return null;
}
return new Date(this.created.getTime());
}
@Override
public void setCreated(Date created) {
this.created = new Date(created.getTime());
}
public Date getDeleted() {
if (deleted == null) {
return null;
}
return new Date(this.deleted.getTime());
}
public void setDeleted(Date deleted) {
this.deleted = new Date(deleted.getTime());
}
@Override
public Integer getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public boolean hasValidId() {
return id > 0;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((changed == null) ? 0 : changed.hashCode());
result = prime * result + ((created == null) ? 0 : created.hashCode());
result = prime * result + ((deleted == null) ? 0 : deleted.hashCode());
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
BaseEntity other = (BaseEntity) obj;
if (deleted == null) {
if (other.deleted != null) {
return false;
}
} else if (!deleted.equals(other.deleted)) {
return false;
}
if (id != other.id) {
return false;
}
return true;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("BaseEntity [id=");
stringBuilder.append(id);
stringBuilder.append(", changed=");
stringBuilder.append(changed);
if (deleted != null) {
stringBuilder.append(", deleted=");
stringBuilder.append(deleted);
}
stringBuilder.append("]");
return stringBuilder.toString();
}
public abstract class BaseEntity implements EntityAccessor
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Temporal(TemporalType.TIMESTAMP)
private Date changed;
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@Temporal(TemporalType.TIMESTAMP)
private Date deleted;
public Date getChanged()
{
if (changed == null) {
return null;
}
return new Date(this.changed.getTime());
}
@Override
public void setChanged(Date changed)
{
this.changed = new Date(changed.getTime());
}
public Date getCreated()
{
if (created == null) {
return null;
}
return new Date(this.created.getTime());
}
@Override
public void setCreated(Date created)
{
this.created = new Date(created.getTime());
}
public Date getDeleted()
{
if (deleted == null) {
return null;
}
return new Date(this.deleted.getTime());
}
public void setDeleted(Date deleted)
{
this.deleted = new Date(deleted.getTime());
}
@Override
public Integer getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
@Override
public boolean hasValidId()
{
return id > 0;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((changed == null) ? 0 : changed.hashCode());
result = prime * result + ((created == null) ? 0 : created.hashCode());
result = prime * result + ((deleted == null) ? 0 : deleted.hashCode());
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
BaseEntity other = (BaseEntity) obj;
if (deleted == null) {
if (other.deleted != null) {
return false;
}
}
else if (!deleted.equals(other.deleted)) {
return false;
}
if (id != other.id) {
return false;
}
return true;
}
@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("BaseEntity [id=");
stringBuilder.append(id);
stringBuilder.append(", changed=");
stringBuilder.append(changed);
if (deleted != null) {
stringBuilder.append(", deleted=");
stringBuilder.append(deleted);
}
stringBuilder.append("]");
return stringBuilder.toString();
}
}

@ -10,45 +10,47 @@ import javax.persistence.Table;
/**
* The persistent class for the groupDef database table.
*
*/
@Entity
@Table(name = "groupdef")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@NamedQuery(name = GroupDef.QUERY_FINDALL, query = "SELECT g FROM GroupDef g")
public class GroupDef extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
public final static String QUERY_FINDALL = "GroupDef.findAll";
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 53;
return prime * super.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
return super.equals(obj);
}
@Override
public String toString() {
return "GroupDef [id=" + getId() + ", name=" + name + "]";
}
public class GroupDef extends BaseEntity implements Serializable
{
private static final long serialVersionUID = 1L;
public final static String QUERY_FINDALL = "GroupDef.findAll";
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public int hashCode()
{
final int prime = 53;
return prime * super.hashCode();
}
@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
return super.equals(obj);
}
@Override
public String toString()
{
return "GroupDef [id=" + getId() + ", name=" + name + "]";
}
}

@ -10,7 +10,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.TestPropertySource;
import de.kreth.clubhelper.model.dao.GroupDao;
import de.kreth.clubhelper.model.dao.PersonDao;
@ -19,60 +19,60 @@ import de.kreth.clubhelper.model.data.GroupDef;
import de.kreth.clubhelper.model.data.Person;
@DataJpaTest()
@Sql(scripts = "classpath:data.sql")
class LoadPersonsTest {
@Autowired
PersonDao personDao;
@Autowired
GroupDao groupDao;
private GroupDef aktive;
private GroupDef trainer;
private GroupDef competitor;
private GroupDef admin;
@BeforeEach
void loadGroups() {
aktive = groupDao.findById(1).get();
trainer = groupDao.findById(3).get();
competitor = groupDao.findById(7).get();
admin = groupDao.findById(8).get();
assertNotNull(aktive);
assertNotNull(trainer);
assertNotNull(competitor);
assertNotNull(admin);
}
@Test
void testLoadPerson1() {
Optional<Person> person1 = personDao.findById(1);
assertTrue(person1.isPresent(), "Person with id=1 not found!");
assertTrue(person1.get().isMember(trainer));
}
@Test
void testLoadAll() {
Iterable<Person> all = personDao.findAll();
assertTrue(all.iterator().hasNext(), "No data found in Person");
}
@Test
void insertPerson() {
Person p = new Person();
p.setPrename("prename");
p.setSurname("surname");
p.setBirth(LocalDate.of(1981, 3, 3));
p.setGender(Gender.MALE);
personDao.save(p);
assertNotNull(p.getId());
personDao.delete(p);
}
//@Sql(scripts = "classpath:data.sql")
@TestPropertySource(properties = {
"spring.jpa.hibernate.ddl-auto=none"
})
class LoadPersonsTest
{
@Autowired
PersonDao personDao;
@Autowired
GroupDao groupDao;
private GroupDef aktive;
private GroupDef trainer;
private GroupDef competitor;
private GroupDef admin;
@BeforeEach
void loadGroups()
{
aktive = groupDao.findById(1).get();
trainer = groupDao.findById(3).get();
competitor = groupDao.findById(7).get();
admin = groupDao.findById(8).get();
assertNotNull(aktive);
assertNotNull(trainer);
assertNotNull(competitor);
assertNotNull(admin);
}
@Test
void testLoadPerson1()
{
Optional<Person> person1 = personDao.findById(1);
assertTrue(person1.isPresent(), "Person with id=1 not found!");
assertTrue(person1.get().isMember(trainer));
}
@Test
void testLoadAll()
{
Iterable<Person> all = personDao.findAll();
assertTrue(all.iterator().hasNext(), "No data found in Person");
}
@Test
void insertPerson()
{
Person p = new Person();
p.setPrename("prename");
p.setSurname("surname");
p.setBirth(LocalDate.of(1981, 3, 3));
p.setGender(Gender.MALE);
personDao.save(p);
assertNotNull(p.getId());
personDao.delete(p);
}
}

@ -26,54 +26,58 @@ import de.kreth.clubhelper.model.dao.PersonDao;
import de.kreth.clubhelper.model.data.Gender;
import de.kreth.clubhelper.model.data.Person;
@WebMvcTest(excludeAutoConfiguration = { DataSourceAutoConfiguration.class, JdbcRepositoriesAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class, JdbcTemplateAutoConfiguration.class,
SecurityAutoConfiguration.class })
class PersonControllerTest {
@WebMvcTest(excludeAutoConfiguration =
{
DataSourceAutoConfiguration.class,
JdbcRepositoriesAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class,
SecurityAutoConfiguration.class
})
class PersonControllerTest
{
@Autowired
MockMvc mvc;
@Autowired
MockMvc mvc;
@MockBean
PersonDao personDao;
@MockBean
PersonDao personDao;
private Person p1;
private Person p2;
private Person p1;
@BeforeEach
void initMocks()
{
p1 = new Person();
p1.setId(1);
p1.setPrename("prename");
p1.setSurname("surname");
p1.setBirth(LocalDate.of(2000, 1, 1));
p1.setGender(Gender.MALE);
p2 = new Person();
p2.setId(1);
p2.setPrename("prename");
p2.setSurname("surname");
p2.setBirth(LocalDate.of(2000, 1, 1));
p2.setGender(Gender.MALE);
when(personDao.findAll()).thenReturn(Arrays.asList(p1, p2));
when(personDao.findById(1)).thenReturn(Optional.of(p1));
when(personDao.findById(2)).thenReturn(Optional.of(p2));
}
private Person p2;
@BeforeEach
void initMocks() {
p1 = new Person();
p1.setId(1);
p1.setPrename("prename");
p1.setSurname("surname");
p1.setBirth(LocalDate.of(2000, 1, 1));
p1.setGender(Gender.MALE);
p2 = new Person();
p2.setId(1);
p2.setPrename("prename");
p2.setSurname("surname");
p2.setBirth(LocalDate.of(2000, 1, 1));
p2.setGender(Gender.MALE);
when(personDao.findAll()).thenReturn(Arrays.asList(p1, p2));
when(personDao.findById(1)).thenReturn(Optional.of(p1));
when(personDao.findById(2)).thenReturn(Optional.of(p2));
}
@Test
void callAllPersons() throws Exception {
String jsonListOfPersons = "[{\"id\":1,\"changed\":null,\"created\":null,\"deleted\":null,\"birth\":\"2000-01-01\",\"prename\":\"prename\",\"surname\":\"surname\",\"username\":null,\"password\":null,\"gender\":\"MALE\"},{\"id\":1,\"changed\":null,\"created\":null,\"deleted\":null,\"birth\":\"2000-01-01\",\"prename\":\"prename\",\"surname\":\"surname\",\"username\":null,\"password\":null,\"gender\":\"MALE\"}]";
mvc.perform(get("/person").accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML))
.andExpect(status().isOk()).andExpect(content().string(jsonListOfPersons));
}
@Test
void callPerson1() throws Exception {
String jsonListOfPersons = "{\"id\":1,\"changed\":null,\"created\":null,\"deleted\":null,\"birth\":\"2000-01-01\",\"prename\":\"prename\",\"surname\":\"surname\",\"username\":null,\"password\":null,\"gender\":\"MALE\"}";
mvc.perform(get("/person/1").accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML))
.andExpect(status().isOk()).andExpect(content().string(jsonListOfPersons));
}
@Test
void callAllPersons() throws Exception
{
String jsonListOfPersons = "[{\"id\":1,\"changed\":null,\"created\":null,\"deleted\":null,\"birth\":\"2000-01-01\",\"prename\":\"prename\",\"surname\":\"surname\",\"username\":null,\"password\":null,\"gender\":\"MALE\"},{\"id\":1,\"changed\":null,\"created\":null,\"deleted\":null,\"birth\":\"2000-01-01\",\"prename\":\"prename\",\"surname\":\"surname\",\"username\":null,\"password\":null,\"gender\":\"MALE\"}]";
mvc.perform(get("/person").accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)).andExpect(status().isOk())
.andExpect(content().string(jsonListOfPersons));
}
@Test
void callPerson1() throws Exception
{
String jsonListOfPersons = "{\"id\":1,\"changed\":null,\"created\":null,\"deleted\":null,\"birth\":\"2000-01-01\",\"prename\":\"prename\",\"surname\":\"surname\",\"username\":null,\"password\":null,\"gender\":\"MALE\"}";
mvc.perform(get("/person/1").accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)).andExpect(status().isOk())
.andExpect(content().string(jsonListOfPersons));
}
}

Loading…
Cancel
Save