Search in sources :

Example 16 with Valid

use of javax.validation.Valid in project vboard by voyages-sncf-technologies.

the class UsersController method updateUser.

@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseBody
@Valid
public // Parsing the params in the JSON body requires using a dedicated @RequestBody annotated class instead of simple @RequestParam arguments
User updateUser(@Valid @RequestBody UserParamsUpdate params) {
    permission.ensureEmailMatchesSessionUser(params.getEmail());
    this.logger.debug("Updating user {}", params.getEmail());
    final String email = params.getEmail();
    final String team = params.getTeam();
    final User user = this.userDAO.findByEmail(email);
    if (user == null) {
        throw new NotFoundException("User not found");
    }
    String[] previousList = user.getTeam().split(",");
    List<String> newList = Arrays.asList(team.split(","));
    List<String> removedTeam = new ArrayList<>();
    if (!user.getTeam().isEmpty()) {
        for (String t : previousList) {
            if (!newList.contains(t)) {
                removedTeam.add(t);
            }
        }
    }
    if (!removedTeam.isEmpty()) {
        for (String t : removedTeam) {
            teamsController.removeMember(t, permission.getSessionUser().getUserString());
        }
    }
    user.setTeam(team);
    // unchanged means that the avatar has not been changed by the user and thus no need to change it
    if (!"unchanged".equals(params.getAvatar())) {
        user.setHasCustomAvatar(!"default".equals(params.getAvatar()));
        uploadsManager.saveAvatar(params.getAvatar(), email);
    }
    final String info = params.getInfo();
    user.setInfo(info);
    user.setReceiveNlEmails(params.isReceiveNlEmails());
    user.setReceiveLeaderboardEmails(params.isReceiveLeaderboardEmails());
    user.setReceivePopularPinsEmails(params.isReceivePopularPins());
    user.setReceiveRecapEmails(params.isReceiveRecapEmails());
    try {
        this.logger.debug("User updated: email={} - team={} - info={}", email, team, info);
        this.userDAO.save(user);
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return user;
}
Also used : User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) NotFoundException(com.vsct.vboard.exceptions.NotFoundException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Example 17 with Valid

use of javax.validation.Valid in project vboard by voyages-sncf-technologies.

the class LabelsController method getAllFromPins.

// Return all labels still on any pins
@RequestMapping(value = "/throughPins", method = RequestMethod.GET)
@ResponseBody
@Valid
public Set<Label> getAllFromPins() {
    Set<Label> labels = new HashSet<>();
    Iterable<Pin> pins = this.pinDAO.findAll();
    pins.forEach(p -> {
        if (!isBlank(p.getLabels())) {
            List<String> pinLabels = p.getLabelsAsList();
            pinLabels.forEach(l -> {
                labels.add(new Label(l));
                this.labelDAO.save(new Label(l));
            });
        }
    });
    return labels;
}
Also used : Pin(com.vsct.vboard.models.Pin) Label(com.vsct.vboard.models.Label) HashSet(java.util.HashSet) Valid(javax.validation.Valid) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 18 with Valid

use of javax.validation.Valid in project vboard by voyages-sncf-technologies.

the class CommentsController method removeComment.

@RequestMapping(value = "", method = RequestMethod.DELETE)
@ResponseBody
@Valid
public Comment removeComment(@RequestParam(value = "id") String id) {
    Comment comment;
    try {
        comment = this.commentDAO.findById(id).orElse(null);
        if (comment != null) {
            // Check if the user can update this comment (or throw an exception)
            permission.ensureUserHasRightsToAlterPin(comment.getAuthor());
            this.commentDAO.delete(comment);
            String pinId = comment.getPinId();
            Pin pin = this.pinDAO.findByPinId(pinId);
            if (pin != null) {
                pin.decreaseCommentsNumber();
                this.pinDAO.save(pin);
            }
            // Decrease the number of comments for the given pin in elasticsearch
            this.elsClient.removeComment(pinId);
            this.logger.debug("deleteComment: id={}", id);
            // Update the stats
            this.gamification.updateStats(permission.getSessionUser());
        } else {
            throw new VBoardException("Comment does not exist or already deleted");
        }
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return comment;
}
Also used : Comment(com.vsct.vboard.models.Comment) VBoardException(com.vsct.vboard.models.VBoardException) Pin(com.vsct.vboard.models.Pin) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Example 19 with Valid

use of javax.validation.Valid in project vboard by voyages-sncf-technologies.

the class TeamsController method addNewTeam.

@RequestMapping(value = "", method = RequestMethod.POST)
@ResponseBody
@Valid
public Team addNewTeam(@Valid @RequestBody String teamName) {
    teamName = JavaUtils.extractJSONObject(teamName, "name");
    Team team = this.teamDAO.findByName(teamName);
    if (team == null) {
        team = new Team(teamName);
    }
    // The member who created the team (or set the team if it already exists) is added to it as a member
    team.addMember(permission.getSessionUser().getUserString());
    try {
        this.logger.debug("addNewTeam: {}", teamName);
        this.teamDAO.save(team);
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return team;
}
Also used : VBoardException(com.vsct.vboard.models.VBoardException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Team(com.vsct.vboard.models.Team) Valid(javax.validation.Valid)

Example 20 with Valid

use of javax.validation.Valid in project vboard by voyages-sncf-technologies.

the class UsersController method updateFavoriteLabels.

@RequestMapping(value = "/favoriteLabels", method = RequestMethod.POST)
@ResponseBody
@Valid
public User updateFavoriteLabels(@Valid @RequestBody String labels) {
    User user = permission.getSessionUser();
    labels = JavaUtils.extractJSONObject(labels, "labels");
    user.setFavoriteLabels(labels);
    try {
        this.userDAO.save(user);
        this.logger.debug("User {} updated its favorite labels: {}", user.getNiceName(), labels);
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return user;
}
Also used : User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Aggregations

Valid (javax.validation.Valid)59 Response (javax.ws.rs.core.Response)14 Logger (org.slf4j.Logger)14 LoggerFactory (org.slf4j.LoggerFactory)14 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)14 List (java.util.List)12 java.util (java.util)11 Autowired (org.springframework.beans.factory.annotation.Autowired)11 Timed (com.codahale.metrics.annotation.Timed)10 User (com.vsct.vboard.models.User)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 Path (javax.ws.rs.Path)10 Produces (javax.ws.rs.Produces)10 DateTime (org.joda.time.DateTime)10 VBoardException (com.vsct.vboard.models.VBoardException)9 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 Optional (java.util.Optional)9 Collectors (java.util.stream.Collectors)9 Collectors.toList (java.util.stream.Collectors.toList)9