mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-05-29 22:17:50 +08:00
Limit tags to two and integrate post publish
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { computed } from 'vue'
|
import { computed } from 'vue'
|
||||||
import { API_BASE_URL } from '../main'
|
import { API_BASE_URL, toast } from '../main'
|
||||||
import Dropdown from './Dropdown.vue'
|
import Dropdown from './Dropdown.vue'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -23,7 +23,13 @@ export default {
|
|||||||
|
|
||||||
const selected = computed({
|
const selected = computed({
|
||||||
get: () => props.modelValue,
|
get: () => props.modelValue,
|
||||||
set: v => emit('update:modelValue', v)
|
set: v => {
|
||||||
|
if (Array.isArray(v) && v.length > 2) {
|
||||||
|
toast.error('最多选择两个标签')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
emit('update:modelValue', v)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return { fetchTags, selected }
|
return { fetchTags, selected }
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ import { ref } from 'vue'
|
|||||||
import PostEditor from '../components/PostEditor.vue'
|
import PostEditor from '../components/PostEditor.vue'
|
||||||
import CategorySelect from '../components/CategorySelect.vue'
|
import CategorySelect from '../components/CategorySelect.vue'
|
||||||
import TagSelect from '../components/TagSelect.vue'
|
import TagSelect from '../components/TagSelect.vue'
|
||||||
|
import { API_BASE_URL, toast } from '../main'
|
||||||
|
import { getToken } from '../utils/auth'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'NewPostPageView',
|
name: 'NewPostPageView',
|
||||||
@@ -33,12 +35,50 @@ export default {
|
|||||||
const content = ref('')
|
const content = ref('')
|
||||||
const selectedCategory = ref('')
|
const selectedCategory = ref('')
|
||||||
const selectedTags = ref([])
|
const selectedTags = ref([])
|
||||||
const submitPost = () => {
|
const submitPost = async () => {
|
||||||
console.log('title:', title.value)
|
if (!title.value.trim()) {
|
||||||
console.log('content:', content.value)
|
toast.error('标题不能为空')
|
||||||
console.log('category:', selectedCategory.value)
|
return
|
||||||
console.log('tags:', selectedTags.value)
|
}
|
||||||
// 在此处可以调用接口提交帖子
|
if (!content.value.trim()) {
|
||||||
|
toast.error('内容不能为空')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!selectedCategory.value) {
|
||||||
|
toast.error('请选择分类')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (selectedTags.value.length === 0) {
|
||||||
|
toast.error('请选择标签')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const token = getToken()
|
||||||
|
const res = await fetch(`${API_BASE_URL}/api/posts`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${token}`
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
title: title.value,
|
||||||
|
content: content.value,
|
||||||
|
categoryId: selectedCategory.value,
|
||||||
|
tagIds: selectedTags.value
|
||||||
|
})
|
||||||
|
})
|
||||||
|
const data = await res.json()
|
||||||
|
if (res.ok) {
|
||||||
|
toast.success('发布成功')
|
||||||
|
if (data.id) {
|
||||||
|
window.location.href = `/posts/${data.id}`
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
toast.error(data.error || '发布失败')
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
toast.error('发布失败')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return { title, content, selectedCategory, selectedTags, submitPost }
|
return { title, content, selectedCategory, selectedTags, submitPost }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,6 +53,9 @@ public class PostService {
|
|||||||
if (tagIds == null || tagIds.isEmpty()) {
|
if (tagIds == null || tagIds.isEmpty()) {
|
||||||
throw new IllegalArgumentException("At least one tag required");
|
throw new IllegalArgumentException("At least one tag required");
|
||||||
}
|
}
|
||||||
|
if (tagIds.size() > 2) {
|
||||||
|
throw new IllegalArgumentException("At most two tags allowed");
|
||||||
|
}
|
||||||
User author = userRepository.findByUsername(username)
|
User author = userRepository.findByUsername(username)
|
||||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||||
Category category = categoryRepository.findById(categoryId)
|
Category category = categoryRepository.findById(categoryId)
|
||||||
|
|||||||
Reference in New Issue
Block a user