Skip to content

Commit

Permalink
Updated way of getting / updating an address related to a student (#88)
Browse files Browse the repository at this point in the history
- To get the address of a student it can only be done through a GET to /api/v1/students/{docket}
- To update the address of a student it can only be done thorugh a POST to /api/v1/students/{docket} with all other information required for a student
  • Loading branch information
gibarsin committed Feb 3, 2017
1 parent 6beb159 commit a7ad6dc
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import ar.edu.itba.paw.shared.StudentFilter;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

public interface StudentDao {
Expand Down Expand Up @@ -54,9 +53,8 @@ public interface StudentDao {
/**
* Update student
* @param student The new student
* @return The Result code of update
*/
boolean update(Student student);
void update(Student student);

/**
* Gets the students that comply to a list of filters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Set;

public interface StudentService {

Expand Down Expand Up @@ -40,10 +39,8 @@ public interface StudentService {
* Update student
*
* @param newStudent The new student
* @param oldStudent The old student
* @return true if the student was updated; else false
*/
boolean update(Student newStudent, Student oldStudent);
void update(Student newStudent);

/**
* Delete the student that matches the given docket.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -103,11 +101,9 @@ public boolean create(final Student student) {
}

@Override
public boolean update(final Student student) {
public void update(final Student student) {
em.merge(student);
LOGGER.debug("[update] - {}", student);

return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ public boolean editGrade(final Grade newGrade, final BigDecimal oldGrade) {

@Transactional
@Override
public boolean update(final Student student, final Student oldStudent) {
public void update(final Student student) {
final Student oldStudent = getByDocket(student.getDocket());

/* Set Remaining information that cannot be updated by the user via this method */
student.setDocket(oldStudent.getDocket());
student.setDni(oldStudent.getDni());
student.setId_seq(oldStudent.getId_seq());
student.setAddress(oldStudent.getAddress());
if(student.getAddress() != null){
student.getAddress().setDni(student.getDni());
}
student.setDocket(oldStudent.getDocket());
student.getAddress().setId_seq(oldStudent.getAddress().getId_seq());
student.setPassword(oldStudent.getPassword());
student.setEmail(oldStudent.getEmail());
student.setRole(oldStudent.getRole());
Expand All @@ -113,7 +112,7 @@ public boolean update(final Student student, final Student oldStudent) {
final List<Course> courses = studentDao.getStudentCourses(oldStudent.getDocket());
student.setStudentCourses(courses);

return studentDao.update(student);
studentDao.update(student);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

import ar.edu.itba.paw.interfaces.CourseService;
import ar.edu.itba.paw.interfaces.StudentService;
import ar.edu.itba.paw.models.*;
import ar.edu.itba.paw.models.Course;
import ar.edu.itba.paw.models.FinalInscription;
import ar.edu.itba.paw.models.Grade;
import ar.edu.itba.paw.models.TranscriptGrade;
import ar.edu.itba.paw.models.users.Student;
import ar.edu.itba.paw.shared.CourseFilter;
import ar.edu.itba.paw.shared.StudentFilter;
Expand Down Expand Up @@ -106,12 +109,13 @@ public Response studentsUpdate(@PathParam("docket") final int docket,
@Valid final StudentsUpdateDTO studentUpdateDTO) {

final Student oldStudent = ss.getByDocket(docket);

if(oldStudent == null) {
return status(Status.NOT_FOUND).build();
}

final Student partialStudent = mapper.convertToStudent(studentUpdateDTO);
ss.update(partialStudent, oldStudent);
partialStudent.setDocket(docket);
ss.update(partialStudent);

return noContent().build();
}
Expand All @@ -123,35 +127,6 @@ public Response studentsDestroy(@PathParam("docket") final int docket) {
return noContent().build();
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{docket}/address")
public Response studentsAddressShow(@PathParam("docket") final int docket){
final Student student = ss.getByDocket(docket);
if(student == null || student.getAddress() == null) {
return status(Status.NOT_FOUND).build();
}
final AddressDTO addressDTO = mapper.convertToAddressDTO(student.getAddress());
return ok(addressDTO).build();
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{docket}/address")
public Response studentsAddressUpdate(
@PathParam("docket") final int docket,
@Valid final AddressDTO addressDTO) {
final Student student = ss.getByDocket(docket);

if(student == null){
return status(Status.NOT_FOUND).build();
}

Address address = mapper.convertToAddress(addressDTO);
ss.editAddress(docket, address);
return ok().build();
}

@GET
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{docket}/courses")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,12 @@ public class StudentShowDTO {
private LocalDate birthday;
private int docket;
private String email;
private AddressDTO address;

public StudentShowDTO() {

}

public StudentShowDTO(Integer dni, String firstName, String lastName, User.Genre genre, LocalDate birthday, int docket, String email) {
this.dni = dni;
this.firstName = firstName;
this.lastName = lastName;
this.genre = genre;
this.birthday = birthday;
this.docket = docket;
this.email = email;
}

public Integer getDni() {
return dni;
}
Expand Down Expand Up @@ -83,4 +74,12 @@ public String getEmail() {
public void setEmail(String email) {
this.email = email;
}

public AddressDTO getAddress() {
return address;
}

public void setAddress(AddressDTO address) {
this.address = address;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

import ar.edu.itba.paw.models.users.User;

import javax.validation.constraints.Digits;
import javax.validation.constraints.Min;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.time.LocalDate;

public class StudentsUpdateDTO {

@Digits(integer=8, fraction=0)
@Min(value = 1)
@NotNull()
private Integer dni;

@NotNull()
Expand All @@ -30,18 +26,14 @@ public class StudentsUpdateDTO {
@XmlJavaTypeAdapter(LocalDateAdapter.class) //TODO: Improve, see if we can validate if the date is incorrect, so an error is returned, instead of mapping it to null
private LocalDate birthday;

@NotNull
@Valid
private AddressDTO address;

public StudentsUpdateDTO() {

}

public StudentsUpdateDTO(Integer dni, String firstName, String lastName, User.Genre genre, LocalDate birthday) {
this.dni = dni;
this.firstName = firstName;
this.lastName = lastName;
this.genre = genre;
this.birthday = birthday;
}

public Integer getDni() {
return dni;
}
Expand Down Expand Up @@ -81,4 +73,12 @@ public LocalDate getBirthday() {
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}

public AddressDTO getAddress() {
return address;
}

public void setAddress(AddressDTO address) {
this.address = address;
}
}

0 comments on commit a7ad6dc

Please sign in to comment.