글쓰기

This commit is contained in:
이진석
2020-02-04 10:53:12 +09:00
parent 6a59d1a541
commit 0ade00525b
6 changed files with 93 additions and 24 deletions

View File

@@ -1,12 +1,12 @@
import axios from 'axios'; import axios from 'axios';
import commonUtil from "../utils/commonUtil";
function getArticles({page = 0, size = 10, q = ''}) { function getArticles({page = 0, size = 10, q = ''}) {
const accessToken = this.$cookie.get('accessToken');
return axios({ return axios({
url: '/api/articles', url: '/api/articles',
headers: { headers: {
'Authorization': 'Bearer ' + accessToken, 'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
}, },
params: { params: {
page, page,
@@ -16,6 +16,22 @@ function getArticles({page = 0, size = 10, q = ''}) {
}); });
} }
function postArticle({title = '', content = ''}) {
return axios({
url: '/api/articles',
method: 'post',
headers: {
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
},
data: {
title,
content
}
});
}
export default { export default {
getArticles getArticles,
postArticle
} }

View File

@@ -1,16 +1,12 @@
import axios from "axios"; import axios from "axios";
export default async function () { export default async function () {
try { await axios({
const token = this.$cookie.get('accessToken'); method: 'get',
const result = await axios({ url: '/api/users',
method: 'get', headers: {
url: '/api/users', 'Authorization': 'Bearer ' + this.$cookie.get('accessToken')
headers: { }
'Authorization': 'Bearer ' + token });
}
});
} catch (e) {
}
} }

View File

@@ -1,5 +1,5 @@
<template> <template>
<div> <div v-if="!pending">
<article v-for="article in articles" v-bind:key="article.id"> <article v-for="article in articles" v-bind:key="article.id">
<span>{{ article.title }}</span> <span>{{ article.title }}</span>
<span>{{ article }}</span> <span>{{ article }}</span>
@@ -13,27 +13,29 @@
<script> <script>
import articleApi from "../../api/articleApi"; import articleApi from "../../api/articleApi";
import authApi from "../../api/authApi"; import authentication from "../../middlewares/authentication";
export default { export default {
name: "List", name: "List",
data() { data() {
return { return {
articles: [], articles: [],
pending: false pending: true
} }
}, },
async beforeCreate() { async beforeCreate() {
try { try {
await authApi.session(this.$cookie.get('accessToken')); await authentication.bind(this)();
} catch (e) { } catch (e) {
alert('토큰이 존재하지 않거나 올바르지 않은 토큰입니다.'); alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
await this.$router.push('/'); await this.$router.replace('/');
return;
} }
try { try {
const result = await articleApi.getArticles.bind(this)({}); const result = await articleApi.getArticles.bind(this)({});
this.articles = result.data; this.articles = result.data;
this.pending = false;
} catch (err) { } catch (err) {
alert('문제가 발생하였습니다.'); alert('문제가 발생하였습니다.');
console.log(err.response); console.log(err.response);

View File

@@ -1,21 +1,67 @@
<template> <template>
<div> <div>
<form ></form> <form @submit="submit">
<div class="row">
<label for="title">Title</label>
<input type="text" id="title" v-model="title">
</div>
<div class="row">
<label for="content">Content</label>
<textarea id="content" v-model="content"></textarea>
</div>
<div class="row">
<button type="submit">확인</button>
</div>
</form>
</div> </div>
</template> </template>
<script> <script>
import articleApi from "../../api/articleApi"; import articleApi from "../../api/articleApi";
import authApi from "../../api/authApi"; import authentication from "../../middlewares/authentication";
export default { export default {
name: "Write", name: "Write",
data() {
return {
title: '',
content: ''
}
},
async beforeCreate() {
try {
authentication.bind(this)();
} catch (e) {
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
await this.$router.replace('/');
}
},
methods: { methods: {
submit: async function(evt) {
evt.preventDefault();
const {title, content} = this;
const data = {
title,
content
};
try {
await articleApi.postArticle.bind(this)(data);
await this.$router.push('/articles');
} catch (err) {
alert('문제가 발생하였습니다.');
console.log(err);
}
}
} }
} }
</script> </script>
<style scoped> <style scoped>
label {
display: block;
}
</style> </style>

View File

@@ -0,0 +1,7 @@
function getAuthenticationHeaderBearer() {
return 'Bearer ' + this.$cookie.get('accessToken');
}
export default {
getAuthenticationHeaderBearer
}

View File

@@ -2,6 +2,7 @@ package com.example.vue.domain.article;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
@@ -20,6 +21,7 @@ public class ArticleController {
} }
@PostMapping @PostMapping
@Transactional
public ArticleResponseDto postArticles(@RequestBody @Valid ArticleRequestDto articleRequestDto) { public ArticleResponseDto postArticles(@RequestBody @Valid ArticleRequestDto articleRequestDto) {
return articleService.save(articleRequestDto); return articleService.save(articleRequestDto);
} }