Skip to content

Commit

Permalink
Merge pull request #142 from cvazquezlos/carlos
Browse files Browse the repository at this point in the history
Carlos
  • Loading branch information
cvazquezlos authored Apr 20, 2017
2 parents 7fcdf17 + 2c870b8 commit 5a14790
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,16 @@ public interface LoanDetail extends Action.Basic, Action.ResoCopy, ResourceCopy.

@JsonView(LoanDetail.class)
@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity<Action> postAction(@RequestBody Action loan, Authentication authentication,
HttpSession session, HttpServletRequest request) {
public ResponseEntity<Action> postAction(@RequestBody Action loan) {

session.setMaxInactiveInterval(-1);
if ((authentication.getName().equals(userService.findOne(loan.getUser().getId()).getName()))
|| (request.isUserInRole("ADMIN"))) {
Date date = new Date();
Resource resource = resourceService.findOne(loan.getResource().getResource().getId());
ResourceCopy resourceCopy = resourceCopyService.findOne(loan.getResource().getID());
int status = logicService.reserveAResource(userService.findOne(loan.getUser().getId()), date, resource,
resourceCopy);
if (status == 0) {
return new ResponseEntity<>(logicService.getAction(), HttpStatus.CREATED);
} else {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Date date = new Date();
Resource resource = resourceService.findOne(loan.getResource().getResource().getId());
ResourceCopy resourceCopy = resourceCopyService.findOne(loan.getResource().getID());
int status = logicService.reserveAResource(userService.findOne(loan.getUser().getId()), date, resource, resourceCopy);
if (status == 0) {
return new ResponseEntity<>(logicService.getAction(), HttpStatus.CREATED);
} else {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -54,11 +53,10 @@ public ResourceCopy postResourceCopy(@RequestBody ResourceCopy resourceCopy, Htt

@JsonView(ResourceCopyDetail.class)
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<Page<ResourceCopy>> getResourceCopies(HttpSession session, @RequestParam (required=false) Integer page) {
public ResponseEntity<List<ResourceCopy>> getResourceCopies(HttpSession session, @RequestParam (required=false) Integer page) {

session.setMaxInactiveInterval(-1);
if(page==null) page=0;
Page<ResourceCopy> resourceCopies = resourceCopyService.findAll(page);
List<ResourceCopy> resourceCopies = resourceCopyService.findAll();
return new ResponseEntity<>(resourceCopies, HttpStatus.OK);
}

Expand Down
12 changes: 6 additions & 6 deletions backend/src/main/java/appSpring/service/LogicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private boolean hasEnoughCopies(Resource resource) {
private boolean hasAnActiveAction(User user, Resource resource) {
List<Action> actions = user.getActions();
for (Action action : actions) {
if (action.getResource().getResource() == resource && action.getDateLoanReturn() == null) {
if (action.getResource().getResource() == resource && action.getDateLoanReturn() == new Date(0)) {
return true;
}
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public Action getAction() {
}

private boolean deleteLoanAvaible(Action loan) {
return (loan.getDateLoanReturn() == null);
return (loan.getDateLoanReturn() == new Date(0));
}

public int deleteALoan(Action loan) {
Expand Down Expand Up @@ -143,11 +143,11 @@ public int createAnUser(User user) {
}

private boolean itIsPossibleToReturn(Action action) {
return ((action.getDateLoanGiven() != null) && (action.getDateLoanReturn() == null));
return ((action.getDateLoanGiven() == new Date(0)) && (action.getDateLoanReturn() == new Date(0)));
}

private boolean itIsPossibleToGive(Action action) {
return ((action.getDateLoanGiven() == null) && (action.getDateLoanReturn() == null));
return ((action.getDateLoanGiven() == new Date(0)) && (action.getDateLoanReturn() == new Date(0)));
}

public int addGiveDate(Action action, Date date) {
Expand Down Expand Up @@ -184,10 +184,10 @@ public int addReturnDate(Action action, Date date) {
List<Action> currentActions = actionService.findByUser(action.getUser());
for (Action currentAction : currentActions) {
Date date1 = currentAction.getDateLoanGiven();
if (date1 == null)
if (date1 == new Date(0))
continue;
Date date3 = currentAction.getDateLoanReturn();
if (date3 != null)
if (date3 == new Date(0))
continue;
date1.setMinutes(date1.getMinutes() + 1);
Date date2 = new Date();
Expand Down
Binary file modified backend/target/appSpring-0.0.1.jar
Binary file not shown.
Binary file modified backend/target/appSpring-0.0.1.jar.original
Binary file not shown.
Binary file modified backend/target/classes/appSpring/service/LogicService.class
Binary file not shown.
47 changes: 26 additions & 21 deletions frontend/src/app/component/public/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,31 @@ export class HomeComponent implements OnInit, DoCheck {
}

reserveResource(resource: Resource) {
console.log('Trying to find a reserve copy avaible of ' + resource.title + '...')
let copy = this.resourceCopyService.getResourceCopyByLocationCode(resource.noReservedCopies[0]);
if (copy === undefined) {
console.log('Not enough copies or user is not allow to make the reserve.');
this.successMessage = false;
this.errorMessage = true;
this.message = 'La reserva no se ha podido realizar.';
} else {
let action: Action;
action = {copy: copy, user: this.user};
console.log(JSON.stringify(action));
this.actionService.postAction(action).subscribe(
response => {
this.errorMessage = false;
this.successMessage = true;
this.message = 'La reserva se ha realizado exitosamente.';
console.log('Reserve successfully completed.');
},
error => console.log('Fail trying to make the reserve.')
);
}
console.log('Trying to find a reserve copy avaible of ' + resource.title + '...');
this.resourceCopyService.getResourceCopies().subscribe(
response => {
for (let copy of response) {
if (copy.locationCode === resource.noReservedCopies[0]) {
let action: Action;
action = {copy: copy, user: this.user};
this.actionService.postAction(action).subscribe(
response => {
this.errorMessage = false;
this.successMessage = true;
this.message = 'La reserva se ha realizado exitosamente.';
console.log('Reserve successfully completed.');
},
error => console.log('Fail trying to make the reserve.')
);
}
}
},
error => {
console.log('Not enough copies or user is not allow to make the reserve.');
this.successMessage = false;
this.errorMessage = true;
this.message = 'La reserva no se ha podido realizar.';
}
);
}
}
28 changes: 4 additions & 24 deletions frontend/src/app/service/resource-copy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {ResourceCopy} from "../model/resource-copy.model";
@Injectable()
export class ResourceCopyService {

resourceCopy: ResourceCopy;
resourceCopies: ResourceCopy[];
authCreds: string;

Expand All @@ -21,30 +20,11 @@ export class ResourceCopyService {
this.authCreds = authCreds;
}

getResourceCopyByLocationCode(locationCode: string) {
let page = 0;
let morePages = true;
getResourceCopies() {
let headers: Headers = new Headers();
headers.append('Authorization', 'Basic ' + this.authCreds);
while (morePages) {
this.http.get(RESOURCECOPY_URL + '?page=' + page, {headers: headers})
.map(response => response.json().content)
.catch(error => Observable.throw('Server error')
).subscribe(
response => {
if (response[1] === undefined) {
morePages = false;
} else {
this.resourceCopies = this.resourceCopies.concat(response);
}
}
);
page++;
}
for (let copy of this.resourceCopies) {
if (copy.locationCode === locationCode)
return copy;
}
return this.http.get(RESOURCECOPY_URL, {headers: headers})
.map(response => response.json())
.catch(error => Observable.throw('Server error'))
}

}

0 comments on commit 5a14790

Please sign in to comment.