Merge pull request 'exchanges-fixes' (#45) from exchanges-fixes into master

Reviewed-on: http://git.plannaplan.pl/filipizydorczyk/backend/pulls/45
This commit is contained in:
Marcin Woźniak 2021-01-18 14:26:03 +01:00
commit 3269a36239
4 changed files with 121 additions and 0 deletions

View File

@ -49,6 +49,7 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import com.plannaplan.responses.mappers.AssignmentResponseMappers; import com.plannaplan.responses.mappers.AssignmentResponseMappers;
import com.plannaplan.responses.models.AssignmentDetailedResponse;
import com.plannaplan.responses.models.AssignmentResponse; import com.plannaplan.responses.models.AssignmentResponse;
/** /**
@ -168,6 +169,28 @@ public class CommisionController extends TokenBasedController {
} }
return new ResponseEntity<>(result, HttpStatus.OK); return new ResponseEntity<>(result, HttpStatus.OK);
}
/**
* @return list of user latests assignmets
* @throws UserNotFoundException if user was not found bny token
*/
@GetMapping("/user/assignments")
@ApiOperation("Return list of latest user commision assignments. User is recognized via token")
public ResponseEntity<List<AssignmentDetailedResponse>> getAllAssignmets() throws UserNotFoundException {
final User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
final Optional<Commision> latestCommision = this.commisionService.getNewestCommision(user);
if (latestCommision.isEmpty()) {
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
}
final List<AssignmentDetailedResponse> response = AssignmentResponseMappers
.mapAssignmetnToDetialedResponse(latestCommision.get().getAssignments());
return new ResponseEntity<>(response, HttpStatus.OK);
} }
/** /**

View File

@ -4,10 +4,13 @@ import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Course; import com.plannaplan.entities.Course;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.responses.models.AssignmentDetailedResponse;
import com.plannaplan.responses.models.AssignmentResponse; import com.plannaplan.responses.models.AssignmentResponse;
import com.plannaplan.types.GroupType; import com.plannaplan.types.GroupType;
@ -69,4 +72,15 @@ public class AssignmentResponseMappers {
} }
return response; return response;
} }
/**
* map assignmetn to detailed response
*
* @param assignments list of assignments to map
* @return list of responses
*/
public static final List<AssignmentDetailedResponse> mapAssignmetnToDetialedResponse(List<Assignment> assignments) {
return assignments.stream().filter(Objects::nonNull).map(AssignmentDetailedResponse::new)
.collect(Collectors.toList());
}
} }

View File

@ -0,0 +1,71 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Groups;
/**
* Assignment detailed response for api
*/
public class AssignmentDetailedResponse {
public Long id;
public String name;
private int day;
private String time;
private String endTime;
private String lecturer;
/**
* @param assignment Assignment instance to map
*/
public AssignmentDetailedResponse(Assignment assignment) {
final Groups group = assignment.getGroup();
this.id = assignment.getId();
this.name = group.getCourseId().getName();
this.day = group.getDay().label;
this.time = group.getTimeString();
this.endTime = group.getEndTimeString();
this.lecturer = group.getLecturer().toString();
}
/**
* @return day as a value from 0-6
*/
public int getDay() {
return day;
}
/**
* @return lecturer string fromated
*/
public String getLecturer() {
return lecturer;
}
/**
* @return time formated string
*/
public String getEndTime() {
return endTime;
}
/**
* @return time formated string
*/
public String getTime() {
return time;
}
/**
* @return name of course that group belongs to
*/
public String getName() {
return this.name;
}
/**
* @return db id of assignment
*/
public Long getId() {
return this.id;
}
}

View File

@ -47,6 +47,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/user"; private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/user";
private static final String GET_SOMEONE_COMMISIONS_ENDPOINT = "/api/v1/commisions/user"; private static final String GET_SOMEONE_COMMISIONS_ENDPOINT = "/api/v1/commisions/user";
private static final String GET_USER_SCHEDULE_ENDPOINT = "/api/v1/commisions/user/schedule"; private static final String GET_USER_SCHEDULE_ENDPOINT = "/api/v1/commisions/user/schedule";
private static final String GET_ASSIGNMENTS_ENDPOINT = "/api/v1/commisions/user/assignments";
private static final String EXPORT_DATA = "/api/v1/commisions/export/csv"; private static final String EXPORT_DATA = "/api/v1/commisions/export/csv";
@ -307,4 +308,16 @@ public class CommisionControllerTest extends AbstractControllerTest {
mockMvc.perform(get(EXPORT_DATA).header("Authorization", "Bearer " + token)).andExpect(status().isOk()) mockMvc.perform(get(EXPORT_DATA).header("Authorization", "Bearer " + token)).andExpect(status().isOk())
.andReturn().getResponse().getContentAsString().contains("user_id, zaj_cykl_id, gr_nr")); .andReturn().getResponse().getContentAsString().contains("user_id, zaj_cykl_id, gr_nr"));
} }
@Test
public void shouldRetrunOkForAssignmentEnpoint() throws Exception {
this.checkUsers();
final User admin = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(admin).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_ASSIGNMENTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
} }