use of org.c4sg.entity.Badge in project c4sg-services by Code4SocialGood.
the class BadgeServiceImpl method findProjectsForHero.
@Override
public List<ProjectDTO> findProjectsForHero(Integer userId) {
// To display it in Heroes page
System.out.println("findProjectsForHero called...");
List<Badge> badges = badgeDAO.findByUser_IdOrderByCreatedTimeAsc(userId);
List<ProjectDTO> projectDtos = new ArrayList<ProjectDTO>();
List<Project> projects = new ArrayList<Project>();
for (Badge badge : badges) {
projects.add(badge.getProject());
System.out.println("findProjectsForHero called..." + badge.getProject().getName());
}
projectDtos = projectMapper.getDtosFromEntities(projects);
return projectDtos;
}
use of org.c4sg.entity.Badge in project c4sg-services by Code4SocialGood.
the class BadgeServiceImpl method isBadgeGiven.
private void isBadgeGiven(Integer userId, Integer projectId) throws UserProjectException {
System.out.println("isBadgeGiven called...");
Badge badge = badgeDAO.findByUser_IdAndProject_Id(userId, projectId);
if (java.util.Objects.nonNull(badge) && badge.getUser().getId().equals(userId) && badge.getProject().getId().equals(projectId)) {
throw new UserProjectException("Record already exist");
}
}
use of org.c4sg.entity.Badge in project c4sg-services by Code4SocialGood.
the class BadgeServiceImpl method saveBadge.
@Override
public Badge saveBadge(Integer userId, Integer projectId) {
/* Volunteer Recognition - Give badge to the volunteer(accepted applicant)
* Send out email to the volunteer
* Save it to the database
*/
System.out.println("save Badge called...");
User user = userDAO.findById(userId);
requireNonNull(user, "Invalid User Id");
Project project = projectDAO.findById(projectId);
requireNonNull(project, "Invalid Project Id");
Integer orgId = project.getOrganization().getId();
Organization org = organizationDAO.findOne(orgId);
List<User> users = userDAO.findByOrgId(orgId);
User orgUser = users.get(0);
isBadgeGiven(userId, projectId);
Badge heroBadge = new Badge();
heroBadge.setUser(user);
heroBadge.setProject(project);
heroBadge = badgeDAO.save(heroBadge);
System.out.println("Hero Badge Given to" + userId + "," + projectId);
// send email to the volunteer
Map<String, Object> contextVolunteer = new HashMap<String, Object>();
contextVolunteer.put("heroesLink", urlService.getHeroUrl());
contextVolunteer.put("org", org);
contextVolunteer.put("orgUser", orgUser);
contextVolunteer.put("project", project);
contextVolunteer.put("projectLink", urlService.getProjectUrl(project.getId()));
asyncEmailService.sendWithContext(Constants.C4SG_ADDRESS, user.getEmail(), orgUser.getEmail(), Constants.SUBJECT_HERO_USER, Constants.TEMPLATE_HERO_USER, contextVolunteer);
System.out.println("Hero Badge email sent: Project=" + project.getId() + " ; ApplicantEmail=" + user.getEmail());
return heroBadge;
}
use of org.c4sg.entity.Badge in project c4sg-services by Code4SocialGood.
the class ProjectController method giveBadge.
// TODO: Replace explicit user{id} with AuthN user id.
@CrossOrigin
@RequestMapping(value = "/{id}/users/{userId}/badge", method = RequestMethod.POST)
@ApiOperation(value = "Give out badge for a volunteer")
@ApiResponses(value = { @ApiResponse(code = 404, message = "ID of project or user invalid") })
public Badge giveBadge(@ApiParam(value = "ID of user", required = true) @PathVariable("userId") Integer userId, @ApiParam(value = "ID of project", required = true) @PathVariable("id") Integer projectId) {
System.out.println("************** ProjectController.giveBadge()" + ": userId=" + userId + "; projectId=" + projectId + " **************");
Badge badge = null;
try {
badge = badgeService.saveBadge(userId, projectId);
} catch (NullPointerException e) {
throw new NotFoundException("ID of project or user invalid");
} catch (Exception e) {
throw e;
}
return badge;
}
Aggregations