diff --git a/frontend/quasar.conf.js b/frontend/quasar.conf.js index 70be8d8..b557976 100644 --- a/frontend/quasar.conf.js +++ b/frontend/quasar.conf.js @@ -62,7 +62,10 @@ module.exports = function (ctx) { 'QAvatar', 'QSpinnerPuff', 'QExpansionItem', - 'QParallax' + 'QParallax', + 'QEditor', + 'QSelect', + 'QField', ], directives: [ @@ -103,7 +106,7 @@ module.exports = function (ctx) { devServer: { // https: true, port: 8081, - open: true // opens browser window automatically + open: false // opens browser window automatically }, // animations: 'all' --- includes all animations diff --git a/frontend/src/pages/Cache.vue b/frontend/src/pages/Cache.vue new file mode 100644 index 0000000..513310e --- /dev/null +++ b/frontend/src/pages/Cache.vue @@ -0,0 +1,78 @@ + + + + + diff --git a/frontend/src/pages/CacheView.vue b/frontend/src/pages/CacheView.vue deleted file mode 100644 index a2d5ae5..0000000 --- a/frontend/src/pages/CacheView.vue +++ /dev/null @@ -1,202 +0,0 @@ - - - - diff --git a/frontend/src/pages/Login.vue b/frontend/src/pages/Login.vue index 13c0770..cb0e8cc 100644 --- a/frontend/src/pages/Login.vue +++ b/frontend/src/pages/Login.vue @@ -21,8 +21,8 @@
{ - // this.$router.push('/') - // }) const data = { username: this.user.username, password: this.user.password - } //JSON.stringify(this.user); + }; + console.log("GET http://localhost:8080/api/login/ - json: " + JSON.stringify(data)); - this.$axios.post('http://localhost:8080/api/login', data) // TODO muss GET mit AUTH Header werden + + this.$axios.post('http://localhost:8080/api/login', data) .then((response) => { + // TODO Catch clause funktioniert nicht mehr wenn Statuscode zurückgegeben wird. -> hier if/else mit HTTP-Codes console.log("GET/POST http://localhost:8080/api/login/ - response: " + response.data); - //this.user.token = response.data; - localStorage.setItem('userToken', JSON.stringify(response)); - this.isAuthenticated(); + localStorage.setItem('userToken', JSON.stringify(response.data)); + this.evalAuthentication(); }) .catch((error) => { console.log("error: " + error); this.credentialsDialog=true; }) }, - isAuthenticated: function () { - console.log("isAuthenticated()"); - console.log(localStorage.getItem('userToken')); - if (localStorage.getItem('userToken')) { // TODO hier muss Abfrage mit API, z.B. /api/user?token="ME" stattfinden. - this.user.isAuthenticated = true; - } else { - this.user.isAuthenticated = false; - } + evalAuthentication: function () { + this.$store.commit('auth/SET_AUTHENTICATED'); }, logout: function () { console.log("logout()"); - console.log(localStorage.getItem('userToken')); - localStorage.removeItem('userToken'); - console.log(localStorage.getItem('userToken')); - this.isAuthenticated() + console.log(JSON.parse(localStorage.getItem('userToken'))); + this.$axios.get('http://localhost:8080/api/logout', { + params: { + token: JSON.parse(localStorage.getItem('userToken')) + } + }) + .then((response) => { + console.log("GET/POST http://localhost:8080/api/logout/ - response: " + response.data); + localStorage.removeItem('userToken'); + this.evalAuthentication(); + }) + .catch((error) => { + }); }, }, }; diff --git a/frontend/src/pages/Overview.vue b/frontend/src/pages/Overview.vue index c45872f..7560bed 100644 --- a/frontend/src/pages/Overview.vue +++ b/frontend/src/pages/Overview.vue @@ -9,7 +9,6 @@ active-bg-color="bg-grey-1" active-color="cyan-14" indicator-color="cyan-14" - narrow-indicator switch-indicator > @@ -20,30 +19,41 @@
+ + - + + - + - {{ cache.name }} - - {{ cache.rankingPoints }} Punkte / - Size {{ cache.stationen.length }} - - {{ cache.description.substring(0,80) }} ... + {{ cache.description }} + - + + + + + + + + @@ -53,6 +63,7 @@ +
@@ -66,35 +77,24 @@ */ + + diff --git a/frontend/src/pages/StationView.vue b/frontend/src/pages/StationView.vue new file mode 100644 index 0000000..f81a729 --- /dev/null +++ b/frontend/src/pages/StationView.vue @@ -0,0 +1,65 @@ + + + + + diff --git a/frontend/src/pages/TestCaches.vue b/frontend/src/pages/TestCaches.vue deleted file mode 100644 index 59402e7..0000000 --- a/frontend/src/pages/TestCaches.vue +++ /dev/null @@ -1,84 +0,0 @@ - - - - - diff --git a/frontend/src/router/routes.js b/frontend/src/router/routes.js index 62e6125..1fa2204 100644 --- a/frontend/src/router/routes.js +++ b/frontend/src/router/routes.js @@ -11,14 +11,19 @@ const routes = [ children: [{ path: "", component: () => import("pages/Overview.vue") }] }, { - path: "/testcaches/", + path: "/cache/", component: () => import("layouts/MyLayout.vue"), - children: [{path: "", component: () => import("pages/TestCaches.vue")}] + children: [{ path: "", component: () => import("pages/Cache.vue") }] }, { - path: "/cacheview/", + path: "/station/", component: () => import("layouts/MyLayout.vue"), - children: [{ path: "", component: () => import("pages/CacheView.vue") }] + children: [{ path: "", component: () => import("pages/StationEdit.vue") }] + }, + { + path: "/station/:id", + component: () => import("layouts/MyLayout.vue"), + children: [{ path: "", component: () => import("pages/StationView.vue") }] }, { path: "/ranking/", diff --git a/frontend/src/store/auth/mutations.js b/frontend/src/store/auth/mutations.js index ffa7222..46266c0 100644 --- a/frontend/src/store/auth/mutations.js +++ b/frontend/src/store/auth/mutations.js @@ -1,38 +1,18 @@ import axios from 'axios' -/* -export function someMutation (state) { -} -*/ -export const evalAuth = (state) => { - console.log("isAuthenticated()"); - console.log(localStorage.getItem('userToken')); - if (localStorage.getItem('userToken')) { // TODO hier muss Abfrage mit API, z.B. /api/user?token="ME" stattfinden. - state.user.isAuthenticated = true; +export const SET_AUTHENTICATED = (state) => { + console.log("SET_AUTHENTICATED()"); + console.log(JSON.parse(localStorage.getItem('userToken'))); + if (localStorage.getItem('userToken')) { + state.userAuthenticated.isAuthenticated = true; } else { - state.user.isAuthenticated = false; + state.userAuthenticated.isAuthenticated = false; } }; -export const logout = (state) => { - console.log("logout()"); - console.log(localStorage.getItem('userToken')); - localStorage.removeItem('userToken'); - console.log(localStorage.getItem('userToken')); - state.commit(evalAuth(state)); +export const SET_USER_PROPERTIES = (state, user) => { + console.log("SET_USER_PROPERTIES()"); + console.log("still todo!"); }; -export const login = (state) => { - const data = { - username: state.user.username, - password: state.user.password - }; - console.log("GET http://localhost:8080/api/login/ - json: " + JSON.stringify(data)); - this.state.$axios.post('http://localhost:8080/api/login', data) // TODO muss GET mit AUTH Header werden - .then((response) => { - console.log("GET/POST http://localhost:8080/api/login/ - response: " + response.data); - localStorage.setItem('userToken', JSON.stringify(response)); - state.commit(evalAuth(state)); - }) - .catch((error) => { - console.log("error: " + error); - //errorDialog = true; - }) +export const SET_LOGOUT = (state) => { + console.log("SET_LOGOUT()"); + state.userAuthenticated = null; }; diff --git a/frontend/src/store/auth/state.js b/frontend/src/store/auth/state.js index 307952d..0e9581a 100644 --- a/frontend/src/store/auth/state.js +++ b/frontend/src/store/auth/state.js @@ -1,7 +1,12 @@ export default { - user: { - username: "moximoti", - password: "1234", - isAuthenticated: false + userAuthenticated: { + id: 1, + firstname: "t", + lastname: "v", + username: "mo", + email: "test@user.com", + rankingPointsSum: 345, + isAuthenticated: false, + isAdmin: false, }, } diff --git a/frontend/src/store/cacheCollector/actions.js b/frontend/src/store/cacheCollector/actions.js new file mode 100644 index 0000000..4787a5f --- /dev/null +++ b/frontend/src/store/cacheCollector/actions.js @@ -0,0 +1,4 @@ +/* +export function someAction (context) { +} +*/ diff --git a/frontend/src/store/cacheCollector/getters.js b/frontend/src/store/cacheCollector/getters.js new file mode 100644 index 0000000..cc054a3 --- /dev/null +++ b/frontend/src/store/cacheCollector/getters.js @@ -0,0 +1,4 @@ +/* +export function someGetter (state) { +} +*/ diff --git a/frontend/src/store/cacheCollector/index.js b/frontend/src/store/cacheCollector/index.js new file mode 100644 index 0000000..b41a219 --- /dev/null +++ b/frontend/src/store/cacheCollector/index.js @@ -0,0 +1,12 @@ +import state from './state' +import * as getters from './getters' +import * as mutations from './mutations' +import * as actions from './actions' + +export default { + namespaced: true, + state, + getters, + mutations, + actions +} diff --git a/frontend/src/store/cacheCollector/mutations.js b/frontend/src/store/cacheCollector/mutations.js new file mode 100644 index 0000000..63131e2 --- /dev/null +++ b/frontend/src/store/cacheCollector/mutations.js @@ -0,0 +1,4 @@ +/* +export function someMutation (state) { +} +*/ diff --git a/frontend/src/store/cacheCollector/state.js b/frontend/src/store/cacheCollector/state.js new file mode 100644 index 0000000..40b8bc3 --- /dev/null +++ b/frontend/src/store/cacheCollector/state.js @@ -0,0 +1,3 @@ +export default { + // +} diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js index dfbcdb1..8eb6c71 100644 --- a/frontend/src/store/index.js +++ b/frontend/src/store/index.js @@ -2,6 +2,7 @@ import Vue from "vue"; import Vuex from "vuex"; import Axios from "axios"; import auth from "./auth" +import cacheCollector from "./cacheCollector" // import example from './module-example' @@ -12,24 +13,11 @@ Vue.use(Vuex, Axios); * directly export the Store instantiation */ -// export default function(/* { ssrContext } */) { -// const Store = new Vuex.Store({ -// modules: { -// // example -// }, -// -// // enable strict mode (adds overhead!) -// // for dev mode only -// strict: process.env.DEV -// }); -// -// return Store; -// } - export default function (/* { ssrContext } */) { const Store = new Vuex.Store({ modules: { - auth + auth, + cacheCollector }, // enable strict mode (adds overhead!) @@ -45,8 +33,12 @@ export default function (/* { ssrContext } */) { */ if (process.env.DEV && module.hot) { module.hot.accept(['./auth'], () => { - const newShowcase = require('./auth').default; - store.hotUpdate({ modules: { showcase: newShowcase } }) + const auth = require('./auth').default; + store.hotUpdate({ modules: { auth: newAuth } }) + }); + module.hot.accept(['./cacheCollector'], () => { + const cacheCollector = require('./cacheCollector').default; + store.hotUpdate({ modules: { cacheCollector: newCacheCollector } }) }) } diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index a245f13..6499b71 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -40,16 +40,13 @@ public class Controller { @Autowired UserRepository userRepository; - @Autowired - StationReihenfolgeRepository stationReihenfolgeRepository; - private AtomicLong counter = new AtomicLong(); @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/allCaches") @ResponseBody - public String getAllCaches() { - return new Gson().toJson(cacheRepository.findAll()); + public ResponseEntity getAllCaches() { + return ResponseEntity.status(200).body(new Gson().toJson(cacheRepository.findAll())); } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @@ -59,34 +56,37 @@ public class Controller { if (user.getUsername() == null || user.getPassword() == null) { System.out.println(user.getUsername()); System.out.println(user.getPassword()); - return ResponseEntity.status(401).body(null); + return ResponseEntity.status(400).body("Username or password cant be null"); } if (userRepository.findByUsername(user.getUsername()) == null) { - return ResponseEntity.status(401).body(null); + return ResponseEntity.status(404).body("User was not found"); } if (BCrypt.checkpw(user.getPassword(), userRepository.findByUsername(user.getUsername()).getPassword())) { String token = user.getUsername() + BCrypt.hashpw(String.valueOf(System.currentTimeMillis() + counter.incrementAndGet()), BCrypt.gensalt()); - System.out.println(token); String hashedToken = BCrypt.hashpw(token, BCrypt.gensalt()); userRepository.findByUsername(user.getUsername()).setToken(hashedToken); userRepository.save(userRepository.findByUsername(user.getUsername())); - return ResponseEntity.ok(new Gson().toJson(token)); + //return ResponseEntity.ok(new Gson().toJson(token)); + return ResponseEntity.status(200).body(token); } - return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(null); + return ResponseEntity.status(400).body("Es ist ein Fehler aufgetreten"); } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/startCache") @ResponseBody - public String startCache(@RequestParam(value = "token", defaultValue = "-1") String token, - @RequestParam String cacheID) throws IllegalParameterException { + public ResponseEntity startCache(@RequestParam(value = "token", defaultValue = "-1") String token, + @RequestParam String cacheID) throws IllegalParameterException { if (!token.equals("-1")) { // ein angemeldeter user startet den cache(es werden zwei parameter übergeben) Bearbeitet bearbeitet = new Bearbeitet(); User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); + if (user == null) { + return ResponseEntity.status(404).body("User was not found"); + } bearbeitet.setUser(user); Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); @@ -94,10 +94,10 @@ public class Controller { Cache cache = cacheOptional.get(); bearbeitet.setCache(cache); - Station startStation = cache.getStartStation(); + Station startStation = cache.getStationen().get(0); bearbeitet.setAktuelleStation(startStation); } else { - throw new IllegalParameterException("There is no cache with the ID " + cacheID); + return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID); } Optional cacheAccesDefinitionOptional = @@ -106,20 +106,20 @@ public class Controller { CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); } else { - throw new IllegalParameterException("There is no cacheAccesDefinition with the ID " + 0); + return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 0); } bearbeitetRepository.save(bearbeitet); - return new Gson().toJson(bearbeitet); + return ResponseEntity.status(200).body(new Gson().toJson(bearbeitet)); } else { // kein angemeldeter User startet den cache(es wird nur der cache als parameter übergeben) Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); if (cacheOptional.isPresent()) { Cache cache = cacheOptional.get(); - return new Gson().toJson(cache); + return ResponseEntity.status(200).body(new Gson().toJson(cache)); } else { - throw new IllegalParameterException("There is no cache with the ID " + cacheID); + return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID); } } } @@ -127,155 +127,138 @@ public class Controller { @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/logout") @ResponseBody - boolean logout(@RequestParam String token) { - + public ResponseEntity logout(@RequestParam String token) { +// System.out.println("logout"); User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); +// System.out.println(token); +// System.out.println(user.getToken()); + if (user == null || user.getToken().isEmpty()) { + return ResponseEntity.status(404).body("User was not found"); + } user.setToken(null); userRepository.save(user); - return true; - } - - @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose - @RequestMapping("/api/createStation") - @ResponseBody - String createStation(@RequestParam String description, - @RequestParam String lattitude, - @RequestParam String longitude, - @RequestParam String solution) throws IllegalParameterException { - - if (description.length() == 0 || lattitude.length() == 0 || longitude.length() == 0 || solution.length() == 0) { - throw new IllegalParameterException("Fields can´t be empty"); - } - - double latti; - double longi; - - try { - latti = Double.valueOf(lattitude); - if (latti < -90 || latti > 90) { - throw new IllegalParameterException("Lattitude has to be in the range of -90 to 90 degrees"); - } - } catch (NumberFormatException e) { - throw new NumberFormatException("Lattitude has to be a decimal number"); - } - - try { - longi = Double.valueOf(longitude); - if (longi < -180 || longi > 180) { - throw new IllegalParameterException("Longitude has to be in the range of -180 to 180 degrees"); - } - } catch (NumberFormatException e) { - throw new NumberFormatException("Longitude has to be a decimal number"); - } - - Station station = new Station(); - station.setDescription(description); - station.setLattitude(latti); - station.setLongitude(longi); - station.setSolution(solution); - Random r = new Random(); - int low = 100000; - int high = 1000000; - int code = r.nextInt(high - low) + low; - station.setCode(code); - - stationRepository.save(station); - - return new Gson().toJson(station); - - } - - @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose - @RequestMapping("/api/checkAdmin") - @ResponseBody - boolean checkAdmin(@RequestParam String token) { - User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); - for (Role role : user.getRoles()) { - if (role.getId() == 0) { // is admin - return true; - } - } - return false; - } - - @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose - @RequestMapping("/api/getAllStations") - @ResponseBody - String getAllStations() { - return new Gson().toJson(stationRepository.findAll()); + return ResponseEntity.status(200).body("Token was deleted"); } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/createCache") @ResponseBody - String createCache(@RequestParam String description, - @RequestParam String name, - @RequestParam String rankingPoints, - @RequestParam(value = "rewardID", defaultValue = "-1") String rewardID, - @RequestParam List stationen) throws IllegalParameterException { + public ResponseEntity createCache(@RequestBody Cache cache) { - if (description.length() == 0 || name.length() == 0 || rankingPoints.length() == 0 || stationen.size() == 0) { - throw new IllegalParameterException("Fields can´t be empty"); - } - - for (Cache cache : cacheRepository.findAll()) { - if (cache.getName().equals(name)) { - throw new IllegalParameterException("name is already taken"); + // Stationen werden in die Datenbank eingetragen + for (Station station : cache.getStationen()) { + ResponseEntity response = createStation(station); + if (response.getStatusCodeValue() == 400) { + deleteStationen(cache); + return response; } } - int points; - - try { - points = Integer.valueOf(rankingPoints); - if (points < 0) { - throw new IllegalParameterException("Ranking points has to be a positive number"); - } - } catch (NumberFormatException e) { - throw new NumberFormatException("Ranking points has to be an integer"); + // Caches werden in die Datenbank eingetragen + if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || cache.getRankingPoints() == 0.0 || cache.getStationen().size() == 0) { + deleteStationen(cache); + return ResponseEntity.status(400).body("cache fields can´t be empty"); } - Optional rewardOptional = rewardRepository.findById(Integer.valueOf(rewardID)); - Reward reward = rewardOptional.orElse(null); + for (Cache cache1 : cacheRepository.findAll()) { + if (cache1.getName().equals(cache.getName())) { + deleteStationen(cache); + return ResponseEntity.status(400).body("name is already taken"); + } + } - Cache cache = new Cache(); - cache.setDescription(description); - cache.setName(name); - cache.setRankingPoints(Integer.valueOf(rankingPoints)); - cache.setReward(reward); - cache.setStartStation(stationen.get(0)); - cache.setStationen(stationen); + if (cache.getRankingPoints() < 0) { + deleteStationen(cache); + return ResponseEntity.status(400).body("Ranking points has to be a positive number"); + } cacheRepository.save(cache); - for (int i = 0; i + 1 < stationen.size(); i++) { - StationReihenfolge stationReihenfolge = new StationReihenfolge(); - stationReihenfolge.setCache(cache); - stationReihenfolge.setStation(stationen.get(i)); - stationReihenfolge.setNachfolgeStation(stationen.get(i + 1)); - stationReihenfolgeRepository.save(stationReihenfolge); + return ResponseEntity.status(200).body(new Gson().toJson(cache)); + } + + public ResponseEntity createStation(Station station) { + + if (station.getDescription().length() == 0 || station.getLattitude() == 0.0 || station.getLongitude() == 0.0 || station.getSolution().length() == 0) { + return ResponseEntity.status(400).body("station fields can´t be empty"); } - return new Gson().toJson(cache); + if (station.getLattitude() < -90 || station.getLattitude() > 90) { + return ResponseEntity.status(400).body("Lattitude has to be between -90 and 90 Degree"); + } + + if (station.getLongitude() < -180 || station.getLongitude() > 180) { + return ResponseEntity.status(400).body("Longitude has to be in the range of -180 to 180 degrees"); + } + + Random r = new Random(); + int low = 100000; + int high = 1000000; + int code = 0; + boolean unique = false; + while (!unique) { + code = r.nextInt(high - low) + low; + unique = true; + for (Station station1 : stationRepository.findAll()) { + if (station1.getCode() == code) { + unique = false; + } + } + } + + station.setCode(code); + + stationRepository.save(station); + + return ResponseEntity.status(200).body(new Gson().toJson(station)); + } + + public void deleteStationen(Cache cache) { + for (Station station : cache.getStationen()) { + try { + stationRepository.delete(station); + } catch (IllegalArgumentException e) { // station is null + // do nothing + } + } + } + + + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose + @RequestMapping("/api/checkAdmin") + @ResponseBody + public ResponseEntity checkAdmin(@RequestParam String token) { + User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); + if (user == null) { + return ResponseEntity.status(404).body("User was not found"); + } + for (Role role : user.getRoles()) { + if (role.getId() == 0) { // is admin + return ResponseEntity.status(200).body("User is Admin"); + } + } + return ResponseEntity.status(401).body("User is no Admin"); + } + + //Bis hier + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose + @RequestMapping("/api/getAllStations") + @ResponseBody + public ResponseEntity getAllStations() { + return ResponseEntity.status(200).body(new Gson().toJson(stationRepository.findAll())); } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/deleteCache") @ResponseBody - boolean deleteCache(@RequestParam String cacheID) throws IllegalParameterException { + public ResponseEntity deleteCache(@RequestParam String cacheID) { Optional optionalCache = cacheRepository.findById(Integer.valueOf(cacheID)); if (!optionalCache.isPresent()) { - throw new IllegalParameterException("There is no cache with the ID " + cacheID); + return ResponseEntity.status(404).body(new Gson().toJson("There is no cache with the ID " + cacheID)); } Cache cache = optionalCache.get(); - for (StationReihenfolge stationReihenfolge : stationReihenfolgeRepository.findAll()) { - if (stationReihenfolge.getCache().getId() == cache.getId()) { - stationReihenfolgeRepository.delete(stationReihenfolge); - } - } - for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) { if (bearbeitet.getCache().getId() == cache.getId()) { bearbeitetRepository.delete(bearbeitet); @@ -284,29 +267,53 @@ public class Controller { cacheRepository.delete(cache); - return true; + return ResponseEntity.status(200).body(new Gson().toJson(true)); } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/getMyCaches") @ResponseBody - String getMyCaches(@RequestParam String token) { - User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); - ArrayList bearbeitetList = new ArrayList<>(); + public ResponseEntity getMyCaches(@RequestParam String token) { + try { + User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); + if (user != null) { + ArrayList bearbeitetList = new ArrayList<>(); - for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) { - if (bearbeitet.getUser().getId() == user.getId()) { - bearbeitetList.add(bearbeitet); + for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) { + if (bearbeitet.getUser().getId() == user.getId()) { + bearbeitetList.add(bearbeitet); + } + } + return ResponseEntity.status(200).body(new Gson().toJson(bearbeitetList)); + } else { + return ResponseEntity.status(404).body("User was not found in the database"); } + } catch (StringIndexOutOfBoundsException e) { + return ResponseEntity.status(400).body("Invalid token"); } - return new Gson().toJson(bearbeitetList); } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/getRankingList") @ResponseBody - String getRankingList() { - return new Gson().toJson(userRepository.getRankingList()); + public ResponseEntity getRankingList() { + return ResponseEntity.status(200).body(new Gson().toJson(userRepository.getRankingList())); + } + + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose + @RequestMapping("/api/getUser") + @ResponseBody + public ResponseEntity getUser(@RequestParam String token) { + try { + User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); + if (user != null) { + return ResponseEntity.status(200).body(new Gson().toJson(user)); + } else { + return ResponseEntity.status(404).body("User was not found in the database"); + } + } catch (StringIndexOutOfBoundsException e) { + return ResponseEntity.status(400).body("Invalid token"); + } } } diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/Cache.java b/src/main/java/hhn/labsw/bugageocaching/entities/Cache.java index 6846b04..465b2cc 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/Cache.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/Cache.java @@ -23,9 +23,6 @@ public class Cache { @ManyToOne private Reward reward; - @ManyToOne - private Station startStation; - public Cache() { } @@ -68,7 +65,7 @@ public class Cache { public void setStationen(List stationen) { this.stationen = stationen; } - + public Reward getReward() { return reward; } @@ -76,12 +73,4 @@ public class Cache { public void setReward(Reward reward) { this.reward = reward; } - - public Station getStartStation() { - return startStation; - } - - public void setStartStation(Station startStation) { - this.startStation = startStation; - } } diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/StationReihenfolge.java b/src/main/java/hhn/labsw/bugageocaching/entities/StationReihenfolge.java deleted file mode 100644 index b51ef51..0000000 --- a/src/main/java/hhn/labsw/bugageocaching/entities/StationReihenfolge.java +++ /dev/null @@ -1,53 +0,0 @@ -package hhn.labsw.bugageocaching.entities; - -import javax.persistence.*; - -@Entity -@Table -public class StationReihenfolge { - - @Id - @GeneratedValue - private int id; - - @OneToOne - private Cache cache; - - @OneToOne - private Station station; - - @OneToOne - private Station nachfolgeStation; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public Cache getCache() { - return cache; - } - - public void setCache(Cache cache) { - this.cache = cache; - } - - public Station getStation() { - return station; - } - - public void setStation(Station station) { - this.station = station; - } - - public Station getNachfolgeStation() { - return nachfolgeStation; - } - - public void setNachfolgeStation(Station nachfolgeStation) { - this.nachfolgeStation = nachfolgeStation; - } -} diff --git a/src/main/java/hhn/labsw/bugageocaching/repositories/StationReihenfolgeRepository.java b/src/main/java/hhn/labsw/bugageocaching/repositories/StationReihenfolgeRepository.java deleted file mode 100644 index 8897965..0000000 --- a/src/main/java/hhn/labsw/bugageocaching/repositories/StationReihenfolgeRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package hhn.labsw.bugageocaching.repositories; - -import hhn.labsw.bugageocaching.entities.StationReihenfolge; -import org.springframework.data.repository.CrudRepository; - -public interface StationReihenfolgeRepository extends CrudRepository { -}