2021-01-06 13:40:04 +01:00
package com.plannaplan.controllers ;
2021-01-07 16:51:35 +01:00
import java.util.List ;
2021-01-06 13:40:04 +01:00
import java.util.Map ;
2021-01-06 14:34:25 +01:00
import java.util.Optional ;
2021-01-10 10:57:33 +01:00
import java.util.stream.Collectors ;
2021-01-06 13:40:04 +01:00
import com.plannaplan.App ;
2021-01-06 14:34:25 +01:00
import com.plannaplan.entities.Assignment ;
2021-01-06 14:50:34 +01:00
import com.plannaplan.entities.Exchange ;
2021-01-06 14:34:25 +01:00
import com.plannaplan.entities.Groups ;
import com.plannaplan.entities.User ;
2021-01-06 16:23:18 +01:00
import com.plannaplan.exceptions.UserNotFoundException ;
2021-01-07 16:51:35 +01:00
import com.plannaplan.responses.mappers.ExchangeResponseMappers ;
import com.plannaplan.responses.models.ExchangeResponse ;
2021-01-06 14:50:34 +01:00
import com.plannaplan.services.AssignmentService ;
import com.plannaplan.services.ExchangeService ;
import com.plannaplan.services.GroupService ;
2021-01-20 14:36:09 +01:00
import com.plannaplan.types.GroupType ;
2021-01-06 14:34:25 +01:00
2021-01-06 14:50:34 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
2021-01-06 14:34:25 +01:00
import org.springframework.http.HttpStatus ;
2021-01-06 13:40:04 +01:00
import org.springframework.http.ResponseEntity ;
import org.springframework.web.bind.annotation.CrossOrigin ;
import org.springframework.web.bind.annotation.PostMapping ;
2021-01-07 16:01:25 +01:00
import org.springframework.web.bind.annotation.DeleteMapping ;
2021-01-07 16:51:35 +01:00
import org.springframework.web.bind.annotation.GetMapping ;
2021-01-07 16:01:25 +01:00
import org.springframework.web.bind.annotation.PathVariable ;
2021-01-06 13:40:04 +01:00
import org.springframework.web.bind.annotation.RequestBody ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RestController ;
import io.swagger.annotations.Api ;
import io.swagger.annotations.ApiOperation ;
2021-01-10 15:59:26 +01:00
import io.swagger.annotations.ApiParam ;
2021-01-06 13:40:04 +01:00
2021-01-16 14:40:17 +01:00
/ * *
* Rest controller to Exchange related endpoints . More detailed api docs is
* available via swagger
* /
2021-01-06 13:40:04 +01:00
@RestController
@CrossOrigin
@RequestMapping ( " /api/ " + App . API_VERSION + " /exchanges " )
@Api ( tags = { " Exchange " } , value = " Exchange " , description = " Endpoint to exchange with accepted assignments. " )
2021-01-16 14:40:17 +01:00
public class ExchangeController extends TokenBasedController {
2021-01-06 14:50:34 +01:00
@Autowired
private GroupService groupService ;
@Autowired
private AssignmentService assignmentService ;
2021-01-12 13:04:57 +01:00
2021-01-06 14:50:34 +01:00
@Autowired
private ExchangeService exchangeService ;
2021-01-16 14:40:17 +01:00
/ * *
* @param exchangeRequest mapped json body with requierd paramas ( groupid and
* assignment )
* @return was job successfull
* @throws UserNotFoundException if user was not found
* /
2021-01-06 13:40:04 +01:00
@PostMapping ( " /exchange " )
2021-01-10 15:59:26 +01:00
@ApiOperation ( value = " Creates exchange offer. " )
2021-01-12 13:04:57 +01:00
public ResponseEntity < String > createExchange (
2021-01-16 14:40:17 +01:00
@ApiParam ( value = " Json object that contains assignment to trade and desired group. For example: { \" assignment \" : 3, \" group \" :32 } " ) @RequestBody Map < String , Long > exchangeRequest )
2021-01-06 16:23:18 +01:00
throws UserNotFoundException {
2021-01-06 14:34:25 +01:00
2021-01-16 14:40:17 +01:00
final User asker = this . getCurrentUser ( ) . orElseThrow ( ( ) - > new UserNotFoundException ( " Invalid token " ) ) ;
2021-01-06 14:50:34 +01:00
final Long assignmentId = exchangeRequest . get ( " assignment " ) ;
final Long groupId = exchangeRequest . get ( " group " ) ;
2021-01-16 14:40:17 +01:00
final List < Long > ownedGroups = asker . getStudentRegisteredGrups ( ) . stream ( ) . map ( Groups : : getId )
. collect ( Collectors . toList ( ) ) ;
2021-01-10 10:57:33 +01:00
2021-01-16 14:40:17 +01:00
if ( ownedGroups . contains ( groupId ) ) {
2021-01-10 10:57:33 +01:00
return new ResponseEntity < > ( " User has already got this group. " , HttpStatus . BAD_REQUEST ) ;
}
2021-01-06 14:50:34 +01:00
2021-01-16 14:40:17 +01:00
if ( assignmentId = = null | | groupId = = null ) {
2021-01-06 14:50:34 +01:00
return new ResponseEntity < > ( " Some of values are missing " , HttpStatus . BAD_REQUEST ) ;
2021-01-06 14:34:25 +01:00
}
2021-01-06 14:50:34 +01:00
final Optional < Assignment > assignment = this . assignmentService . getById ( assignmentId ) ;
final Optional < Groups > group = this . groupService . getGroupById ( groupId ) ;
2021-01-12 13:04:57 +01:00
2021-01-16 14:40:17 +01:00
if ( assignment . isEmpty ( ) | | group . isEmpty ( ) ) {
2021-01-06 14:50:34 +01:00
return new ResponseEntity < > ( " Some of provided value does not exist. " , HttpStatus . BAD_REQUEST ) ;
}
2021-01-06 16:23:18 +01:00
final Assignment assignmentInstance = assignment . get ( ) ;
2021-01-06 16:50:08 +01:00
final Groups groupInstance = group . get ( ) ;
2021-01-06 16:23:18 +01:00
2021-01-18 16:07:48 +01:00
if ( assignmentInstance . getGroup ( ) . getCourseId ( ) ! = null
2021-01-20 14:36:09 +01:00
& & ! assignmentInstance . getGroup ( ) . getCourseId ( ) . getId ( ) . equals ( groupInstance . getCourseId ( ) . getId ( ) ) ) {
2021-01-19 23:54:17 +01:00
System . out . println ( assignmentInstance . getGroup ( ) . getCourseId ( ) . getId ( ) ) ;
2021-01-19 23:59:30 +01:00
System . out . println ( groupInstance . getCourseId ( ) . getId ( ) ) ;
2021-01-18 15:51:58 +01:00
return new ResponseEntity < > ( " You can performe exchange only within one course. " , HttpStatus . BAD_REQUEST ) ;
}
2021-01-20 14:36:09 +01:00
if ( GroupType . isLectureOrClass ( assignmentInstance . getGroup ( ) . getType ( ) ) ! = GroupType
. isLectureOrClass ( groupInstance . getType ( ) ) ) {
2021-01-18 15:51:58 +01:00
return new ResponseEntity < > ( " You can't exchange lecture to class and otherwise. " , HttpStatus . BAD_REQUEST ) ;
}
2021-01-16 14:40:17 +01:00
if ( ! ( assignmentInstance . getCommision ( ) . getCommisionOwner ( ) . getId ( ) . equals ( asker . getId ( ) )
& & assignmentInstance . isAccepted ( ) ) ) {
return new ResponseEntity < > (
" Some of problems appeared. Check if you have access to given assignment and if it is accepted or the exchange has not been already added. " ,
HttpStatus . BAD_REQUEST ) ;
2021-01-06 16:23:18 +01:00
}
2021-01-06 14:50:34 +01:00
2021-01-06 16:50:08 +01:00
this . exchangeService . save ( new Exchange ( assignmentInstance , groupInstance ) ) ;
2021-01-06 14:34:25 +01:00
2021-01-06 16:23:18 +01:00
return new ResponseEntity < > ( " Success " , HttpStatus . OK ) ;
2021-01-06 13:40:04 +01:00
}
2021-01-06 16:50:08 +01:00
2021-01-16 14:40:17 +01:00
/ * *
* @param offerId id to delwete from db
* @return was jub successful
* @throws UserNotFoundException if user was not found
* /
2021-01-07 16:01:25 +01:00
@DeleteMapping ( " /exchange/{id} " )
@ApiOperation ( value = " Delete exchange offer " )
public ResponseEntity < String > deleteExchange ( @PathVariable ( name = " id " , required = false ) Long offerId )
throws UserNotFoundException {
2021-01-16 14:40:17 +01:00
final User asker = this . getCurrentUser ( ) . orElseThrow ( ( ) - > new UserNotFoundException ( " Invalid token " ) ) ;
2021-01-07 16:01:25 +01:00
final Optional < Exchange > exchange = this . exchangeService . getById ( offerId ) ;
2021-01-12 13:04:57 +01:00
2021-01-16 14:40:17 +01:00
if ( exchange . isEmpty ( ) ) {
return new ResponseEntity < > ( " Given offer does not exist. " , HttpStatus . BAD_REQUEST ) ;
2021-01-07 16:01:25 +01:00
}
final Exchange exchangeToDelete = exchange . get ( ) ;
2021-01-16 14:40:17 +01:00
if ( ! ( exchangeToDelete . getOwnedAssignment ( ) . getCommision ( ) . getCommisionOwner ( ) . getId ( ) . equals ( asker . getId ( ) ) ) ) {
return new ResponseEntity < > ( " You have not permission for that action. " , HttpStatus . BAD_REQUEST ) ;
2021-01-07 16:01:25 +01:00
}
2021-01-06 16:50:08 +01:00
2021-01-07 16:01:25 +01:00
this . exchangeService . deleteExchange ( exchangeToDelete ) ;
return new ResponseEntity < > ( " Success " , HttpStatus . OK ) ;
}
2021-01-07 16:51:35 +01:00
2021-01-16 14:40:17 +01:00
/ * *
* @return return all user ' s exchange offers
* @throws UserNotFoundException iF user was not found
* /
2021-01-07 16:51:35 +01:00
@GetMapping ( " /exchange/all " )
@ApiOperation ( value = " Get exchange offers " )
2021-01-16 14:40:17 +01:00
public ResponseEntity < List < ExchangeResponse > > getExchange ( ) throws UserNotFoundException {
2021-01-07 16:51:35 +01:00
2021-01-16 14:40:17 +01:00
final User asker = this . getCurrentUser ( ) . orElseThrow ( ( ) - > new UserNotFoundException ( " Invalid token " ) ) ;
2021-01-12 13:04:57 +01:00
2021-01-07 16:51:35 +01:00
final List < Exchange > response = exchangeService . getByUserId ( asker . getId ( ) ) ;
final List < ExchangeResponse > listOfResponses = ExchangeResponseMappers . mapToDefaultResponse ( response ) ;
2021-01-12 13:04:57 +01:00
2021-01-07 16:51:35 +01:00
return new ResponseEntity < > ( listOfResponses , HttpStatus . OK ) ;
}
2021-01-07 17:02:12 +01:00
2021-01-16 14:40:17 +01:00
/ * *
* @param offerId id of exchange in db
* @return Exchage response
* @throws UserNotFoundException if user was not found
* /
2021-01-07 17:02:12 +01:00
@GetMapping ( " /exchange/{id} " )
2021-01-16 14:40:17 +01:00
@ApiOperation ( value = " Get exchange offer " )
2021-01-07 17:02:12 +01:00
public ResponseEntity < ExchangeResponse > getExchangeById ( @PathVariable ( name = " id " , required = false ) Long offerId )
throws UserNotFoundException {
2021-01-16 14:40:17 +01:00
final User asker = this . getCurrentUser ( ) . orElseThrow ( ( ) - > new UserNotFoundException ( " Invalid token " ) ) ;
2021-01-12 13:04:57 +01:00
2021-01-07 17:02:12 +01:00
final Optional < Exchange > exchange = this . exchangeService . getById ( offerId ) ;
2021-01-16 14:40:17 +01:00
if ( exchange . isEmpty ( ) ) {
return new ResponseEntity < > ( null , HttpStatus . BAD_REQUEST ) ;
2021-01-07 17:02:12 +01:00
}
final Exchange exchangeInstance = exchange . get ( ) ;
2021-01-16 14:40:17 +01:00
if ( ! exchangeInstance . getOwnerId ( ) . equals ( asker . getId ( ) ) ) {
return new ResponseEntity < > ( null , HttpStatus . BAD_REQUEST ) ;
2021-01-07 17:02:12 +01:00
}
2021-01-12 13:04:57 +01:00
2021-01-07 17:02:12 +01:00
return new ResponseEntity < > ( new ExchangeResponse ( exchangeInstance ) , HttpStatus . OK ) ;
}
2021-01-06 13:40:04 +01:00
}