@@ -451,6 +451,189 @@ export const AXIOS = axios.create({
451451})
452452```
453453
454+ #### Using Heroku´s Postgres as Database for Spring Boot backend and Vue.js frontend
455+
456+ First add [ Heroku Postgres database] ( https://elements.heroku.com/addons/heroku-postgresql ) for your Heroku app.
457+
458+ Then follow these instructions on Stackoverflow to configure all needed Environment variables in Heroku: https://stackoverflow.com/a/49978310/4964553
459+
460+ Mind the addition to the backend´s [ pom.xml] ( backend/pom.xml ) described here: https://stackoverflow.com/a/49970142/4964553
461+
462+ Now you´re able to use Spring Data´s magic - all you need is an Interface like [ UserRepository.java] ( backend/src/main/java/de/jonashackt/springbootvuejs/repository/UserRepository.java ) :
463+
464+ ```
465+ package de.jonashackt.springbootvuejs.repository;
466+
467+ import de.jonashackt.springbootvuejs.domain.User;
468+ import org.springframework.data.repository.CrudRepository;
469+ import org.springframework.data.repository.query.Param;
470+
471+ import java.util.List;
472+
473+ public interface UserRepository extends CrudRepository<User, Long> {
474+
475+ List<User> findByLastName(@Param("lastname") String lastname);
476+
477+ List<User> findByFirstName(@Param("firstname") String firstname);
478+
479+ }
480+
481+ ```
482+
483+ Now write your Testcases accordingly like [ UserRepositoryTest.java] ( backend/src/test/java/de/jonashackt/springbootvuejs/repository/UserRepositoryTest.java ) :
484+
485+ ```
486+ package de.jonashackt.springbootvuejs.repository;
487+
488+ import de.jonashackt.springbootvuejs.domain.User;
489+ import org.junit.Before;
490+ import org.junit.Test;
491+ import org.junit.runner.RunWith;
492+ import org.springframework.beans.factory.annotation.Autowired;
493+ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
494+ import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
495+ import org.springframework.test.context.junit4.SpringRunner;
496+
497+ import java.util.List;
498+
499+ import static org.hamcrest.Matchers.contains;
500+ import static org.junit.Assert.*;
501+
502+ @RunWith(SpringRunner.class)
503+ @DataJpaTest
504+ public class UserRepositoryTest {
505+
506+ @Autowired
507+ private TestEntityManager entityManager;
508+
509+ @Autowired
510+ private UserRepository users;
511+
512+ private User norbertSiegmund = new User("Norbert", "Siegmund");
513+ private User jonasHecht = new User("Jonas", "Hecht");
514+
515+ @Before
516+ public void fillSomeDataIntoOurDb() {
517+ // Add new Users to Database
518+ entityManager.persist(norbertSiegmund);
519+ entityManager.persist(jonasHecht);
520+ }
521+
522+ @Test
523+ public void testFindByLastName() throws Exception {
524+ // Search for specific User in Database according to lastname
525+ List<User> usersWithLastNameSiegmund = users.findByLastName("Siegmund");
526+
527+ assertThat(usersWithLastNameSiegmund, contains(norbertSiegmund));
528+ }
529+
530+
531+ @Test
532+ public void testFindByFirstName() throws Exception {
533+ // Search for specific User in Database according to firstname
534+ List<User> usersWithFirstNameJonas = users.findByFirstName("Jonas");
535+
536+ assertThat(usersWithFirstNameJonas, contains(jonasHecht));
537+ }
538+
539+ }
540+ ```
541+
542+ Then include these functionality into your REST-API - see [ BackendController.java] ( backend/src/main/java/de/jonashackt/springbootvuejs/controller/BackendController.java ) :
543+
544+ ```
545+ @RequestMapping(path = "/user", method = RequestMethod.POST)
546+ @ResponseStatus(HttpStatus.CREATED)
547+ public @ResponseBody long addNewUser (@RequestParam String firstName, @RequestParam String lastName) {
548+ User user = new User(firstName, lastName);
549+ userRepository.save(user);
550+
551+ LOG.info(user.toString() + " successfully saved into DB");
552+
553+ return user.getId();
554+ }
555+ ```
556+
557+ and use it from the Vue.js frontend, see [ User.vue] ( frontend/src/components/User.vue ) :
558+
559+ ```
560+ <template>
561+ <div class="user">
562+ <h1>Create User</h1>
563+
564+ <h3>Just some database interaction...</h3>
565+
566+ <input type="text" v-model="user.firstName" placeholder="first name">
567+ <input type="text" v-model="user.lastName" placeholder="last name">
568+
569+ <button @click="createUser()">Create User</button>
570+
571+ <div v-if="showResponse"><h6>User created with Id: {{ response }}</h6></div>
572+
573+ <button v-if="showResponse" @click="retrieveUser()">Retrieve user {{user.id}} data from database</button>
574+
575+ <h4 v-if="showRetrievedUser">Retrieved User {{retrievedUser.firstName}} {{retrievedUser.lastName}}</h4>
576+
577+ </div>
578+ </template>
579+
580+ <script>
581+ // import axios from 'axios'
582+ import {AXIOS} from './http-common'
583+
584+ export default {
585+ name: 'user',
586+
587+ data () {
588+ return {
589+ response: [],
590+ errors: [],
591+ user: {
592+ lastName: '',
593+ firstName: '',
594+ id: 0
595+ },
596+ showResponse: false,
597+ retrievedUser: {},
598+ showRetrievedUser: false
599+ }
600+ },
601+ methods: {
602+ // Fetches posts when the component is created.
603+ createUser () {
604+ var params = new URLSearchParams()
605+ params.append('firstName', this.user.firstName)
606+ params.append('lastName', this.user.lastName)
607+
608+ AXIOS.post(`/user`, params)
609+ .then(response => {
610+ // JSON responses are automatically parsed.
611+ this.response = response.data
612+ this.user.id = response.data
613+ console.log(response.data)
614+ this.showResponse = true
615+ })
616+ .catch(e => {
617+ this.errors.push(e)
618+ })
619+ },
620+ retrieveUser () {
621+ AXIOS.get(`/user/` + this.user.id)
622+ .then(response => {
623+ // JSON responses are automatically parsed.
624+ this.retrievedUser = response.data
625+ console.log(response.data)
626+ this.showRetrievedUser = true
627+ })
628+ .catch(e => {
629+ this.errors.push(e)
630+ })
631+ }
632+ }
633+ }
634+
635+ </script>
636+ ```
454637
455638
456639## Testing
0 commit comments