Final Product
This commit is contained in:
parent
a8cf583569
commit
a9f44c29af
48 changed files with 3908 additions and 581 deletions
|
@ -55,47 +55,56 @@ public class SteamAPI extends PlatformAPI {
|
|||
.queryParam("steamid", userId);
|
||||
|
||||
var games = new ArrayList<APIResponse.Game>();
|
||||
var ownedResponse = rest.exchange(ownedGamesUrl, HttpMethod.GET, entity, GetOwnedGameBody.class).getBody();
|
||||
for (var game : ownedResponse.getResponse().getGames()) {
|
||||
var newGame = new APIResponse.Game();
|
||||
newGame.setPlatformGameId(Integer.toString(game.getAppid()));
|
||||
newGame.setName(game.getName());
|
||||
// Technically this is not the advertised logo url, but it's used be steamcommunity.com
|
||||
// and steamdb.info and it gives better aspect ratios and it means I don't need the random
|
||||
// logo_url field
|
||||
newGame.setThumbnail("https://cdn.cloudflare.steamstatic.com/steam/apps/" + game.getAppid() + "/header.jpg");
|
||||
newGame.setPlayed(game.getPlaytime_forever() > 0);
|
||||
try {
|
||||
var ownedResponse = rest.exchange(ownedGamesUrl, HttpMethod.GET, entity, GetOwnedGameBody.class).getBody();
|
||||
for (var game : ownedResponse.getResponse().getGames()) {
|
||||
var newGame = new APIResponse.Game();
|
||||
newGame.setPlatformGameId(Integer.toString(game.getAppid()));
|
||||
newGame.setName(game.getName());
|
||||
// Technically this is not the advertised logo url, but it's used be steamcommunity.com
|
||||
// and steamdb.info and it gives better aspect ratios and it means I don't need the random
|
||||
// logo_url field
|
||||
newGame.setThumbnail("https://cdn.cloudflare.steamstatic.com/steam/apps/" + game.getAppid() + "/header.jpg");
|
||||
newGame.setPlayed(game.getPlaytime_forever() > 0);
|
||||
|
||||
var achievements = new HashMap<String, APIResponse.Game.Achievement>();
|
||||
var achievements = new HashMap<String, APIResponse.Game.Achievement>();
|
||||
|
||||
var gameSchemaUrl = gameSchemaBaseUrl.cloneBuilder()
|
||||
.queryParam("appid", game.getAppid())
|
||||
.toUriString();
|
||||
var playerAchievementsUrl = playerAchievementsBaseUrl.cloneBuilder()
|
||||
.queryParam("appid", game.getAppid())
|
||||
.toUriString();
|
||||
var gameSchemaUrl = gameSchemaBaseUrl.cloneBuilder()
|
||||
.queryParam("appid", game.getAppid())
|
||||
.toUriString();
|
||||
var playerAchievementsUrl = playerAchievementsBaseUrl.cloneBuilder()
|
||||
.queryParam("appid", game.getAppid())
|
||||
.toUriString();
|
||||
|
||||
var schemaResponse = rest.exchange(gameSchemaUrl, HttpMethod.GET, entity, GetSchemaForGameBody.class).getBody().getGame().getAvailableGameStats();
|
||||
if (schemaResponse != null && schemaResponse.getAchievements() != null) {
|
||||
for (var schema : schemaResponse.getAchievements()) {
|
||||
var achievement = new APIResponse.Game.Achievement();
|
||||
achievement.setName(schema.getDisplayName());
|
||||
achievement.setDescription(schema.getDescription());
|
||||
achievement.setStages(1);
|
||||
achievement.setThumbnail(schema.getIcon());
|
||||
achievements.put(schema.getName(), achievement);
|
||||
}
|
||||
try {
|
||||
var schemaResponse = rest.exchange(gameSchemaUrl, HttpMethod.GET, entity, GetSchemaForGameBody.class).getBody().getGame().getAvailableGameStats();
|
||||
if (schemaResponse != null && schemaResponse.getAchievements() != null) {
|
||||
for (var schema : schemaResponse.getAchievements()) {
|
||||
var achievement = new APIResponse.Game.Achievement();
|
||||
achievement.setName(schema.getDisplayName());
|
||||
achievement.setDescription(schema.getDescription());
|
||||
achievement.setStages(1);
|
||||
achievement.setThumbnail(schema.getIcon());
|
||||
achievements.put(schema.getName(), achievement);
|
||||
}
|
||||
|
||||
var playerAchievementsResponse = rest.exchange(playerAchievementsUrl, HttpMethod.GET, entity, GetPlayerAchievementsBody.class).getBody().getPlayerstats().getAchievements();
|
||||
for (var achievement : playerAchievementsResponse) {
|
||||
achievements.get(achievement.getApiname()).setProgress(achievement.getAchieved());
|
||||
}
|
||||
var playerAchievementsResponse = rest.exchange(playerAchievementsUrl, HttpMethod.GET, entity, GetPlayerAchievementsBody.class).getBody().getPlayerstats().getAchievements();
|
||||
for (var achievement : playerAchievementsResponse) {
|
||||
achievements.get(achievement.getApiname()).setProgress(achievement.getAchieved());
|
||||
}
|
||||
|
||||
newGame.setAchievements(new ArrayList<>(achievements.values()));
|
||||
if (newGame.getAchievements().size() > 0) {
|
||||
games.add(newGame);
|
||||
newGame.setAchievements(new ArrayList<>(achievements.values()));
|
||||
if (newGame.getAchievements().size() > 0) {
|
||||
games.add(newGame);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Forbidden APPID: " + game.getAppid());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
var response = new APIResponse();
|
||||
response.setGames(games);
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package achievements.controllers;
|
||||
|
||||
import achievements.data.APError;
|
||||
import achievements.data.request.RateAchievement;
|
||||
import achievements.services.ImageService;
|
||||
import achievements.services.AchievementService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
@ -19,9 +20,41 @@ public class AchievementController {
|
|||
@Autowired
|
||||
private ImageService imageService;
|
||||
|
||||
@GetMapping(value = "/{achievement}", produces = "application/json")
|
||||
public ResponseEntity getAchievement(@PathVariable("achievement") int achievementId) {
|
||||
var achievement = achievementService.getAchievement(achievementId);
|
||||
if (achievement == null) {
|
||||
return ResponseEntity.badRequest().body(new APError(1, "Failed to get achievement"));
|
||||
} else {
|
||||
return ResponseEntity.ok(achievement);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping(value = "/{achievement}/image")
|
||||
public void getProfilePicture(@PathVariable("achievement") int achievement, HttpServletResponse response) {
|
||||
var icon = achievementService.getIcon(achievement);
|
||||
imageService.send(icon, "achievement", response);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/{achievement}/rating/{user}")
|
||||
public ResponseEntity getRating(@PathVariable("achievement") int achievement, @PathVariable("user") int user) {
|
||||
var rating = achievementService.getRating(achievement, user);
|
||||
if (rating == null) {
|
||||
return ResponseEntity.badRequest().body("{}");
|
||||
} else {
|
||||
return ResponseEntity.ok(rating);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(value = "/{achievement}/rating/{user}")
|
||||
public ResponseEntity setRating(@PathVariable("achievement") int achievement, @PathVariable("user") int user, @RequestBody RateAchievement rating) {
|
||||
var review = achievementService.setRating(achievement, user, rating);
|
||||
if (review == null) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{}");
|
||||
} else if (review.getSessionKey() == null) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(review);
|
||||
} else {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("{}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,11 +65,18 @@ public class AuthController {
|
|||
|
||||
@PostMapping(value = "/refresh", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity refresh(@RequestBody Session key) {
|
||||
if (authService.refresh(key)) {
|
||||
return ResponseEntity.ok("{}");
|
||||
} else {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("{}");
|
||||
if (key.getId() == -1) {
|
||||
if (authService.openAuth()) {
|
||||
if (authService.refresh(key)) {
|
||||
return ResponseEntity.ok(key);
|
||||
} else {
|
||||
return ResponseEntity.ok(authService.session().generate(-1, 0, true));
|
||||
}
|
||||
}
|
||||
} else if (authService.refresh(key)) {
|
||||
return ResponseEntity.ok(key);
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("{}");
|
||||
}
|
||||
|
||||
@PostMapping(value = "/logout", consumes = "application/json")
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package achievements.controllers;
|
||||
|
||||
import achievements.data.importing.ImportPlatform;
|
||||
import achievements.data.importing.ImportUser;
|
||||
import achievements.data.importing.ImportUserPlatform;
|
||||
import achievements.services.ImportService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/import")
|
||||
public class ImportController {
|
||||
|
||||
@Autowired
|
||||
private ImportService importService;
|
||||
|
||||
@PostMapping(value = "/platform", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity createPlatform(@RequestBody ImportPlatform platform) {
|
||||
var response = importService.importPlatform(platform);
|
||||
if (response == 0) {
|
||||
return ResponseEntity.ok("{}");
|
||||
} else {
|
||||
return ResponseEntity.badRequest().body("{}");
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(value = "/user", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity createUser(@RequestBody ImportUser user) {
|
||||
var response = importService.importUser(user);
|
||||
if (response == 0) {
|
||||
return ResponseEntity.ok("{}");
|
||||
} else {
|
||||
return ResponseEntity.badRequest().body("{}");
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(value = "/user/platform", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity addUserToPlatform(@RequestBody ImportUserPlatform userPlatform) {
|
||||
var response = importService.importUserPlatform(userPlatform);
|
||||
if (response == 0) {
|
||||
return ResponseEntity.ok("{}");
|
||||
} else {
|
||||
return ResponseEntity.badRequest().body("{}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -72,7 +72,7 @@ public class UserController {
|
|||
|
||||
@PostMapping(value = "/{user}/platforms/add", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity addPlatformForUser(@PathVariable("user") int userId, @RequestBody AddPlatform request) {
|
||||
var result = userService.addPlatform(userId, request);
|
||||
var result = userService.addPlatform(userId, request, true);
|
||||
if (result == 0) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("{}");
|
||||
} else {
|
||||
|
@ -89,4 +89,14 @@ public class UserController {
|
|||
return ResponseEntity.badRequest().body("{}");
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping(value = "/{user}/noteworthy", produces = "application/json")
|
||||
public ResponseEntity getNoteworthy(@PathVariable("user") int userId) {
|
||||
var result = userService.getNoteworthy(userId);
|
||||
if (result != null) {
|
||||
return ResponseEntity.ok(result);
|
||||
} else {
|
||||
return ResponseEntity.badRequest().body("{}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,59 @@ public class Profile {
|
|||
}
|
||||
}
|
||||
|
||||
public static class Rating {
|
||||
@JsonProperty("achievementId")
|
||||
private int achievementId;
|
||||
@JsonProperty("name")
|
||||
private String name;
|
||||
@JsonProperty("difficulty")
|
||||
private Float difficulty;
|
||||
@JsonProperty("quality")
|
||||
private Float quality;
|
||||
@JsonProperty("review")
|
||||
private String review;
|
||||
|
||||
public int getAchievementId() {
|
||||
return achievementId;
|
||||
}
|
||||
|
||||
public void setAchievementId(int achievementId) {
|
||||
this.achievementId = achievementId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Float getDifficulty() {
|
||||
return difficulty;
|
||||
}
|
||||
|
||||
public void setDifficulty(Float difficulty) {
|
||||
this.difficulty = difficulty;
|
||||
}
|
||||
|
||||
public Float getQuality() {
|
||||
return quality;
|
||||
}
|
||||
|
||||
public void setQuality(Float quality) {
|
||||
this.quality = quality;
|
||||
}
|
||||
|
||||
public String getReview() {
|
||||
return review;
|
||||
}
|
||||
|
||||
public void setReview(String review) {
|
||||
this.review = review;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonProperty("username")
|
||||
private String username;
|
||||
@JsonProperty("completed")
|
||||
|
@ -52,8 +105,8 @@ public class Profile {
|
|||
private List<Achievement> noteworthy;
|
||||
@JsonProperty("platforms")
|
||||
private List<Platform> platforms;
|
||||
/*@JsonProperty("ratings")
|
||||
private List<Rating> ratings;*/
|
||||
@JsonProperty("ratings")
|
||||
private List<Rating> ratings;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
|
@ -102,4 +155,12 @@ public class Profile {
|
|||
public void setPlatforms(List<Platform> platforms) {
|
||||
this.platforms = platforms;
|
||||
}
|
||||
|
||||
public List<Rating> getRatings() {
|
||||
return ratings;
|
||||
}
|
||||
|
||||
public void setRatings(List<Rating> ratings) {
|
||||
this.ratings = ratings;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,14 +16,6 @@ public class Session {
|
|||
@JsonIgnore
|
||||
private boolean used;
|
||||
|
||||
public Session(String key, int id, int hue, boolean admin) {
|
||||
this.key = key;
|
||||
this.id = id;
|
||||
this.hue = hue;
|
||||
this.admin = admin;
|
||||
this.used = false;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
|
|
@ -11,12 +11,6 @@ public class User {
|
|||
@JsonProperty("password")
|
||||
public String password;
|
||||
|
||||
public User(String email, String username, String password) {
|
||||
this.email = email;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package achievements.data.importing;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class ImportPlatform {
|
||||
|
||||
@JsonProperty("userId")
|
||||
private int userId;
|
||||
@JsonProperty("sessionKey")
|
||||
private String sessionKey;
|
||||
@JsonProperty("name")
|
||||
private String name;
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getSessionKey() {
|
||||
return sessionKey;
|
||||
}
|
||||
|
||||
public void setSessionKey(String sessionKey) {
|
||||
this.sessionKey = sessionKey;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package achievements.data.importing;
|
||||
|
||||
import achievements.data.User;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class ImportUser extends User {
|
||||
|
||||
@JsonProperty("userId")
|
||||
private int userId;
|
||||
@JsonProperty("sessionKey")
|
||||
private String sessionKey;
|
||||
@JsonProperty("admin")
|
||||
private boolean admin;
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getSessionKey() {
|
||||
return sessionKey;
|
||||
}
|
||||
|
||||
public void setSessionKey(String sessionKey) {
|
||||
this.sessionKey = sessionKey;
|
||||
}
|
||||
|
||||
public boolean isAdmin() {
|
||||
return admin;
|
||||
}
|
||||
|
||||
public void setAdmin(boolean admin) {
|
||||
this.admin = admin;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package achievements.data.importing;
|
||||
|
||||
import achievements.data.request.AddPlatform;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class ImportUserPlatform extends AddPlatform {
|
||||
|
||||
@JsonProperty("userId")
|
||||
private int userId;
|
||||
@JsonProperty("userEmail")
|
||||
private String userEmail;
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getUserEmail() {
|
||||
return userEmail;
|
||||
}
|
||||
|
||||
public void setUserEmail(String userEmail) {
|
||||
this.userEmail = userEmail;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package achievements.data.request;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class RateAchievement {
|
||||
|
||||
@JsonProperty("sessionKey")
|
||||
private String sessionKey;
|
||||
@JsonProperty("difficulty")
|
||||
private Float difficulty;
|
||||
@JsonProperty("quality")
|
||||
private Float quality;
|
||||
@JsonProperty("review")
|
||||
private String review;
|
||||
|
||||
public String getSessionKey() {
|
||||
return sessionKey;
|
||||
}
|
||||
|
||||
public void setSessionKey(String sessionKey) {
|
||||
this.sessionKey = sessionKey;
|
||||
}
|
||||
|
||||
public Float getDifficulty() {
|
||||
return difficulty;
|
||||
}
|
||||
|
||||
public void setDifficulty(Float difficulty) {
|
||||
this.difficulty = difficulty;
|
||||
}
|
||||
|
||||
public Float getQuality() {
|
||||
return quality;
|
||||
}
|
||||
|
||||
public void setQuality(Float quality) {
|
||||
this.quality = quality;
|
||||
}
|
||||
|
||||
public String getReview() {
|
||||
return review;
|
||||
}
|
||||
|
||||
public void setReview(String review) {
|
||||
this.review = review;
|
||||
}
|
||||
}
|
|
@ -2,8 +2,63 @@ package achievements.data.response.search;
|
|||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Achievement {
|
||||
|
||||
public static class Rating {
|
||||
@JsonProperty("userId")
|
||||
private int userId;
|
||||
@JsonProperty("username")
|
||||
private String username;
|
||||
@JsonProperty("difficulty")
|
||||
private Float difficulty;
|
||||
@JsonProperty("quality")
|
||||
private Float quality;
|
||||
@JsonProperty("review")
|
||||
private String review;
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public Float getDifficulty() {
|
||||
return difficulty;
|
||||
}
|
||||
|
||||
public void setDifficulty(Float difficulty) {
|
||||
this.difficulty = difficulty;
|
||||
}
|
||||
|
||||
public Float getQuality() {
|
||||
return quality;
|
||||
}
|
||||
|
||||
public void setQuality(Float quality) {
|
||||
this.quality = quality;
|
||||
}
|
||||
|
||||
public String getReview() {
|
||||
return review;
|
||||
}
|
||||
|
||||
public void setReview(String review) {
|
||||
this.review = review;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonProperty("ID")
|
||||
private int ID;
|
||||
@JsonProperty("game")
|
||||
|
@ -18,6 +73,8 @@ public class Achievement {
|
|||
private Float difficulty;
|
||||
@JsonProperty("quality")
|
||||
private Float quality;
|
||||
@JsonProperty("ratings")
|
||||
private List<Rating> ratings;
|
||||
|
||||
public int getID() {
|
||||
return ID;
|
||||
|
@ -66,4 +123,12 @@ public class Achievement {
|
|||
public void setQuality(Float quality) {
|
||||
this.quality = quality;
|
||||
}
|
||||
|
||||
public List<Rating> getRatings() {
|
||||
return ratings;
|
||||
}
|
||||
|
||||
public void setRatings(List<Rating> ratings) {
|
||||
this.ratings = ratings;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,11 @@ public class SessionManager {
|
|||
|
||||
public Session generate(int user, int hue, boolean admin) {
|
||||
var key = HashManager.encode(HashManager.generateBytes(16));
|
||||
var session = new Session(key, user, hue, admin);
|
||||
var session = new Session();
|
||||
session.setKey(key);
|
||||
session.setId(user);
|
||||
session.setHue(hue);
|
||||
session.setAdmin(admin);
|
||||
sessions.put(key, session);
|
||||
return session;
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ public class APIService {
|
|||
|
||||
addGameToPlatform.setInt(1, gameId);
|
||||
addGameToPlatform.setInt(2, platformId);
|
||||
addGameToPlatform.setString(3, platformUserId);
|
||||
addGameToPlatform.setString(3, game.getPlatformGameId());
|
||||
addGameToPlatform.execute();
|
||||
|
||||
var gameThumbnail = new File("storage/images/game/" + gameId + "." + getFileType(game.getThumbnail()));
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
package achievements.services;
|
||||
|
||||
import achievements.data.request.RateAchievement;
|
||||
import achievements.data.response.search.Achievement;
|
||||
import achievements.misc.DbConnection;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Service
|
||||
public class AchievementService {
|
||||
|
@ -17,6 +21,9 @@ public class AchievementService {
|
|||
@Autowired
|
||||
private ImageService imageService;
|
||||
|
||||
@Autowired
|
||||
private AuthenticationService authService;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
db = dbs.getConnection();
|
||||
|
@ -31,4 +38,109 @@ public class AchievementService {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Achievement getAchievement(int achievementId) {
|
||||
try {
|
||||
var stmt = db.prepareCall("{call GetAchievement(?)}");
|
||||
stmt.setInt(1, achievementId);
|
||||
|
||||
var result = stmt.executeQuery();
|
||||
if (result.next()) {
|
||||
var achievement = new Achievement();
|
||||
achievement.setID(result.getInt("ID"));
|
||||
achievement.setName(result.getString("Name"));
|
||||
achievement.setCompletion(result.getInt("Completion")); if (result.wasNull()) { achievement.setCompletion(null); }
|
||||
achievement.setDescription(result.getString("Description"));
|
||||
achievement.setDifficulty(result.getFloat("Difficulty")); if (result.wasNull()) { achievement.setDifficulty(null); }
|
||||
achievement.setQuality(result.getFloat("Quality")); if (result.wasNull()) { achievement.setQuality(null); }
|
||||
|
||||
stmt = db.prepareCall("{call GetRatingsForAchievement(?)}");
|
||||
stmt.setInt(1, achievementId);
|
||||
|
||||
var ratings = new ArrayList<Achievement.Rating>();
|
||||
var results = stmt.executeQuery();
|
||||
while (results.next()) {
|
||||
var rating = new Achievement.Rating();
|
||||
rating.setUserId(results.getInt("UserID"));
|
||||
rating.setUsername(results.getString("Username"));
|
||||
rating.setDifficulty(results.getFloat("Difficulty")); if (results.wasNull()) { rating.setDifficulty(null); }
|
||||
rating.setQuality(results.getFloat("Quality")); if (results.wasNull()) { rating.setQuality(null); }
|
||||
rating.setReview(results.getString("Description"));
|
||||
ratings.add(rating);
|
||||
}
|
||||
achievement.setRatings(ratings);
|
||||
return achievement;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public RateAchievement getRating(int achievement, int user) {
|
||||
try {
|
||||
var stmt = db.prepareCall("{call HasProgress(?, ?, ?)}");
|
||||
stmt.setInt(1, user);
|
||||
stmt.setInt(2, achievement);
|
||||
stmt.registerOutParameter(3, Types.BOOLEAN);
|
||||
|
||||
stmt.execute();
|
||||
if (stmt.getBoolean(3)) {
|
||||
stmt = db.prepareCall("{call GetRating(?, ?)}");
|
||||
stmt.setInt(1, user);
|
||||
stmt.setInt(2, achievement);
|
||||
|
||||
var result = stmt.executeQuery();
|
||||
if (result.next()) {
|
||||
var rating = new RateAchievement();
|
||||
rating.setDifficulty(result.getFloat("Difficulty")); if (result.wasNull()) { rating.setDifficulty(null); }
|
||||
rating.setQuality(result.getFloat("Quality")); if (result.wasNull()) { rating.setQuality(null); }
|
||||
rating.setReview(result.getString("Description"));
|
||||
return rating;
|
||||
} else {
|
||||
return new RateAchievement();
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public RateAchievement setRating(int achievementId, int userId, RateAchievement rateAchievement) {
|
||||
if (authService.session().validate(userId, rateAchievement.getSessionKey())) {
|
||||
try {
|
||||
var stmt = db.prepareCall("{call SetRating(?, ?, ?, ?, ?)}");
|
||||
stmt.setInt(1, userId);
|
||||
stmt.setInt(2, achievementId);
|
||||
stmt.setFloat(3, rateAchievement.getDifficulty());
|
||||
stmt.setFloat(4, rateAchievement.getQuality());
|
||||
stmt.setString(5, rateAchievement.getReview());
|
||||
|
||||
stmt.execute();
|
||||
return rateAchievement;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
try {
|
||||
var stmt = db.prepareCall("{call GetRating(?, ?)}");
|
||||
stmt.setInt(1, userId);
|
||||
stmt.setInt(2, achievementId);
|
||||
|
||||
var result = stmt.executeQuery();
|
||||
if (result.next()) {
|
||||
var rating = new RateAchievement();
|
||||
rating.setDifficulty(result.getFloat("Difficulty"));
|
||||
rating.setQuality(result.getFloat("Quality"));
|
||||
rating.setReview(result.getString("Review"));
|
||||
return rating;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,11 +84,14 @@ public class AuthenticationService {
|
|||
public LoginResponse login(User user) {
|
||||
var response = new LoginResponse(-1);
|
||||
try {
|
||||
var statement = db.prepareCall("{call GetUserLogin(?)}");
|
||||
statement.setString(1, user.email);
|
||||
var statement = db.prepareCall("{? = call GetUserLogin(?)}");
|
||||
statement.registerOutParameter(1, Types.INTEGER);
|
||||
statement.setString(2, user.email);
|
||||
|
||||
var result = statement.executeQuery();
|
||||
if (result.next()) {
|
||||
statement.execute();
|
||||
if (statement.getInt(1) == 0) {
|
||||
var result = statement.executeQuery();
|
||||
result.next();
|
||||
var salt = result.getString("Salt");
|
||||
var hash = result.getString("Password");
|
||||
if (Password.validate(salt, user.getPassword(), hash)) {
|
||||
|
@ -115,6 +118,19 @@ public class AuthenticationService {
|
|||
|
||||
public boolean refresh(Session key) { return session.refresh(key.getKey()); }
|
||||
|
||||
public boolean openAuth() {
|
||||
try {
|
||||
var stmt = db.prepareCall("{call HasUser(?)}");
|
||||
stmt.registerOutParameter(1, Types.BOOLEAN);
|
||||
|
||||
stmt.execute();
|
||||
return !stmt.getBoolean(1);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void logout(Session key) {
|
||||
session.remove(key.getKey());
|
||||
}
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
package achievements.services;
|
||||
|
||||
import achievements.data.importing.ImportPlatform;
|
||||
import achievements.data.importing.ImportUser;
|
||||
import achievements.data.importing.ImportUserPlatform;
|
||||
import achievements.misc.DbConnection;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
@Service
|
||||
public class ImportService {
|
||||
|
||||
@Autowired
|
||||
private DbConnection dbs;
|
||||
private Connection db;
|
||||
|
||||
@Autowired
|
||||
private AuthenticationService authService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
db = dbs.getConnection();
|
||||
}
|
||||
|
||||
public int importPlatform(ImportPlatform platform) {
|
||||
if (authService.session().validateAdmin(platform.getUserId(), platform.getSessionKey())) {
|
||||
try {
|
||||
var stmt = db.prepareCall("{call AddPlatform(?, ?)}");
|
||||
stmt.setString(1, platform.getName());
|
||||
stmt.registerOutParameter(2, Types.INTEGER);
|
||||
|
||||
stmt.execute();
|
||||
return 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int importUser(ImportUser user) {
|
||||
if (authService.session().validateAdmin(user.getUserId(), user.getSessionKey())) {
|
||||
try {
|
||||
var response = authService.createUser(user);
|
||||
if (user.isAdmin()) {
|
||||
var stmt = db.prepareCall("{call OpUser(?)}");
|
||||
stmt.setInt(1, response.session.getId());
|
||||
stmt.execute();
|
||||
}
|
||||
|
||||
return 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int importUserPlatform(ImportUserPlatform userPlatform) {
|
||||
if (authService.session().validateAdmin(userPlatform.getUserId(), userPlatform.getSessionKey())) {
|
||||
try {
|
||||
var stmt = db.prepareCall("{call GetIdFromEmail(?, ?)}");
|
||||
stmt.setString(1, userPlatform.getUserEmail());
|
||||
stmt.registerOutParameter(2, Types.INTEGER);
|
||||
|
||||
stmt.execute();
|
||||
return userService.addPlatform(stmt.getInt(2), userPlatform, false);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ import achievements.data.Profile;
|
|||
import achievements.data.request.AddPlatform;
|
||||
import achievements.data.request.RemovePlatform;
|
||||
import achievements.data.request.SetUsername;
|
||||
import achievements.data.response.search.Achievement;
|
||||
import achievements.misc.DbConnection;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -17,6 +18,7 @@ import java.sql.Connection;
|
|||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static achievements.services.ImageService.MIME_TO_EXT;
|
||||
|
||||
|
@ -64,6 +66,8 @@ public class UserService {
|
|||
if (average != null) {
|
||||
profile.setAverage(Integer.parseInt(average));
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -83,6 +87,24 @@ public class UserService {
|
|||
profile.setPlatforms(platforms);
|
||||
}
|
||||
|
||||
{
|
||||
var stmt = db.prepareCall("{call GetRatingsByUser(?)}");
|
||||
stmt.setInt(1, userId);
|
||||
|
||||
var results = stmt.executeQuery();
|
||||
var ratings = new ArrayList<Profile.Rating>();
|
||||
while (results.next()) {
|
||||
var rating = new Profile.Rating();
|
||||
rating.setAchievementId(results.getInt("AchievementID"));
|
||||
rating.setName(results.getString("Name"));
|
||||
rating.setDifficulty(results.getFloat("Difficulty")); if (results.wasNull()) { rating.setDifficulty(null); }
|
||||
rating.setQuality(results.getFloat("Quality")); if (results.wasNull()) { rating.setQuality(null); }
|
||||
rating.setReview(results.getString("Description"));
|
||||
ratings.add(rating);
|
||||
}
|
||||
profile.setRatings(ratings);
|
||||
}
|
||||
|
||||
return profile;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -163,8 +185,8 @@ public class UserService {
|
|||
return "unknown";
|
||||
}
|
||||
|
||||
public int addPlatform(int userId, AddPlatform request) {
|
||||
if (auth.session().validate(userId, request.getSessionKey())) {
|
||||
public int addPlatform(int userId, AddPlatform request, boolean validate) {
|
||||
if (!validate || auth.session().validate(userId, request.getSessionKey())) {
|
||||
try {
|
||||
db.setAutoCommit(false);
|
||||
try {
|
||||
|
@ -211,5 +233,24 @@ public class UserService {
|
|||
return -1;
|
||||
}
|
||||
|
||||
public List<Achievement> getNoteworthy(int userId) {
|
||||
try {
|
||||
var stmt = db.prepareCall("{call GetNoteworthyAchievementsForUser(?)}");
|
||||
stmt.setInt(1, userId);
|
||||
|
||||
var results = stmt.executeQuery();
|
||||
var achievements = new ArrayList<Achievement>();
|
||||
while (results.next()) {
|
||||
var achievement = new Achievement();
|
||||
achievement.setID(results.getInt("ID"));
|
||||
achievement.setName(results.getString("Name"));
|
||||
achievement.setCompletion(results.getInt("Completion"));
|
||||
achievements.add(achievement);
|
||||
}
|
||||
return achievements;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue