Skip to content

Commit

Permalink
close #8
Browse files Browse the repository at this point in the history
  • Loading branch information
migrosdata committed Apr 29, 2019
1 parent 99e9475 commit f7ab02f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/java/ch/hearc/odi/koulutus/business/Course.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.hearc.odi.koulutus.business;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import java.io.Serializable;
import java.util.ArrayList;
Expand Down Expand Up @@ -113,6 +114,7 @@ public void setParticipants(List<Participant> participants) {
}

@ManyToOne
@JsonBackReference
@IndexColumn(name="Programs")
public Program getProgram() {
return program;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ch/hearc/odi/koulutus/business/Program.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.hearc.odi.koulutus.business;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -69,6 +70,7 @@ public void setPrice(int price) {
}

@OneToMany(targetEntity = Course.class, fetch = FetchType.EAGER)
@JsonManagedReference
public List<Course> getCourses() {
return courses;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ch/hearc/odi/koulutus/rest/ProgramResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ public Course addCourse(@PathParam("programId") Long programId, Course course) {
Program program = new Program();
program.setId(programId);
course.setProgram(program);
try {
return persistenceService.createAndPersistCourse(programId, course);
} catch ( ProgramException e){
throw new WebApplicationException(e.getMessage());
}
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,20 @@ public List<Course> getCoursesByProgramId(Long programId) {
return (ArrayList<Course>) courses;
}

public Course createAndPersistCourse(Long programId, Course course) {
public Course createAndPersistCourse(Long programId, Course course) throws ProgramException{
//TODO: fix that
Program program = new Program();
program.setId(programId);
course.setProgram(program);

EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
Program program = entityManager.find(Program.class, programId);
if (program == null) {
logger.error(" program with id " + program.getId() + " not found");
throw new ProgramException(" Program not found");
}
course.setProgram(program);
program.getCourses().add(course);
entityManager.persist(course);
//entityManager.persist(program);
entityManager.getTransaction().commit();
entityManager.close();
logger.info("course with id" + course.getId() + " created" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void createAndPersistAParticipant() throws ParticipantException {
}

@Test
public void createAndPersistACourse() {
public void createAndPersistACourse() throws Exception{
Course course = new Course();
//TODO: fix enum?
course.setQuarter(QuarterEnum.NUMBER_2);
Expand Down

0 comments on commit f7ab02f

Please sign in to comment.