Skip to content

Commit

Permalink
add employee stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Zusel committed Jun 16, 2024
1 parent 86ca094 commit 65830a8
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,29 @@ public void generateEmployee(@RequestBody String count) {
}
employeeService.createEmployees(employees);
}

@GetMapping("/shortnames")
public List<String> getShortNames() {
List<Employee> employees = employeeService.getAllEmployees();
return employees.stream().map(Employee::getShortName).toList();
}

@GetMapping("/getIdForShortName")
public Long getEmployeeIdByShortName(@RequestParam String shortName, HttpServletResponse response) {
List<Employee> employees = employeeService.getAllEmployees();
Long employeeId = employees.stream().filter(e -> e.getShortName().equals(shortName)).map(Employee::getId).findFirst().orElse(null);
if (employeeId == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
return employeeId;
}

@PostMapping("/validate")
public boolean validateEmployee(@RequestBody Employee employee) {
if (employee.getId() != null && employee.getShortName() != null) {
Employee filteredEmployee = employeeService.getEmployeeById(employee.getId());
return filteredEmployee.getShortName().equals(employee.getShortName());
}
return false;
}
}
3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"@mdi/font": "7.0.96",
"axios": "^1.6.8",
"vue": "^3.4.0",
"vuetify": "^3.5.0"
"vuetify": "^3.5.0",
"vuex": "^4.1.0"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.0.4",
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/cmp_AddCustomer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<v-row>
<v-col>
<v-text-field class="inputFields" v-model="postcode"
:rules="[rules.notNull]"
:rules="[rules.notNull, rules.onlynumbers]"
label="Postleitzahl"/>
</v-col>
<v-col>
Expand Down Expand Up @@ -104,7 +104,8 @@ export default {
return 'Email ist ungültig!'
}
}
}
},
onlynumbers: value => /^\d+$/.test(value) || "Nur Zahlen sind erlaubt!"
},
form: null
}
Expand Down
31 changes: 28 additions & 3 deletions frontend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,41 @@
*/

// Plugins
import { registerPlugins } from '@/plugins'
import {registerPlugins} from '@/plugins'

// Components
import App from './App.vue'

// Composables
import { createApp } from 'vue'
import {createApp} from 'vue'
import {createStore} from "vuex";

const store = createStore({
state: {
employeeShortName: null,
employeeId: null,
},
getters: {
getEmployeeShortName(state, getters) {
return getters.employeeShortName;
},
getEmployeeId(state, getters) {
return getters.employeeId;
},
},
mutations: {
setEmployeeShortName(state, employeeShortName) {
state.employeeShortName = employeeShortName;
},
setEmployeeId(state, employeeId) {
state.employeeId = employeeId;
}
}
})

export default store
const app = createApp(App)

registerPlugins(app)

app.use(store)
app.mount('#app')
51 changes: 51 additions & 0 deletions frontend/src/pages/Login.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<template>
<v-container style="height: 100%; align-items: center; display: flex">
<v-container style="border: white solid; width: 80vh">
<v-select label="Mitarbeiter:" v-model="selectedEmployee" :items="employees"/>
<v-btn @click="setEmployee" style="width: 100%">Login</v-btn>
</v-container>
</v-container>

</template>

<script>
import RESTUtils from "@/utils/RESTUtils";
import store from "@/main";
export default {
name: "Login",
data() {
return {
employees: [],
selectedEmployee: null,
}
},
methods: {
getEmployees: function () {
RESTUtils.sendGetRequest("/employee/shortnames")
.then(response => {
this.employees = response.data
})
},
setEmployee: function () {
if (this.selectedEmployee === null) {
this.$root.$refs.vtoast.show(
{
message: 'Naja vielleicht das nächste mal :)',
color: 'failed'
}
)
} else {
RESTUtils.sendGetRequest("/employee/getIdForShortName?shortName=" + this.selectedEmployee)
.then(response => {
store.commit("setEmployeeShortName", this.selectedEmployee)
store.commit("setEmployeeId", response.data)
})
}
}
},
created() {
this.getEmployees();
}
}
</script>
21 changes: 19 additions & 2 deletions frontend/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@
*/

// Composables
import { createRouter, createWebHistory } from 'vue-router/auto'
import { setupLayouts } from 'virtual:generated-layouts'
import {createRouter, createWebHistory} from 'vue-router/auto'
import {setupLayouts} from 'virtual:generated-layouts'
import USERUtils from "@/utils/USERUtils";
import store from "@/main";

const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
extendRoutes: setupLayouts,
})

router.beforeEach((to, from) => {
if (to.name === "/Login") {
return true;
}
if (store.state.employeeId === null || store.state.employeeShortName === null) {
console.log("Login benötigt!")
return "/Login";
}
if (!USERUtils.validateUser(store.state.employeeId, store.state.employeeShortName)) {
console.log("Validation fehlgeschlagen!")
return "/Login";
}
return true;
})

export default router
26 changes: 2 additions & 24 deletions frontend/src/utils/RESTUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,15 @@ const RESTUtils = {
},
sendGetRequest(path) {
return this.sendRequest('GET', path, null)
.catch(error =>
this.$root.$refs.vtoast.show({
message: 'Fehler: ' + error,
color: 'failed'
})
);
},
sendDeleteRequest(path, body) {
return this.sendRequest('DELETE', path, body)
.catch(error =>
this.$root.$refs.vtoast.show({
message: 'Fehler: ' + error,
color: 'failed'
})
);
},
sendPutRequest(path, body) {
return this.sendRequest('PUT', path, body).catch(error =>
this.$root.$refs.vtoast.show({
message: 'Fehler: ' + error,
color: 'failed'
})
);
return this.sendRequest('PUT', path, body)
},
sendPostRequest(path, body) {
return this.sendRequest('POST', path, body).catch(error =>
this.$root.$refs.vtoast.show({
message: 'Fehler: ' + error,
color: 'failed'
})
);
return this.sendRequest('POST', path, body)
}
};

Expand Down
16 changes: 16 additions & 0 deletions frontend/src/utils/USERUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import RESTUtils from "@/utils/RESTUtils";
import {tr} from "vuetify/locale";

const USERUtils = {
async validateUser(id, shortname) {
const employee = {
id: id,
shortName: shortname
}
let response = await RESTUtils.sendPostRequest("/employee/validate", employee)
console.log(response.data)
return response.data
}
}

export default USERUtils;

0 comments on commit 65830a8

Please sign in to comment.