Merge pull request #83 from nagisa77/codex/limit-tags-to-two-and-integrate-publishing-api

Limit tags and integrate post publish
This commit is contained in:
Tim
2025-07-06 01:49:43 +08:00
committed by GitHub
3 changed files with 57 additions and 8 deletions

View File

@@ -4,7 +4,7 @@
<script>
import { computed } from 'vue'
import { API_BASE_URL } from '../main'
import { API_BASE_URL, toast } from '../main'
import Dropdown from './Dropdown.vue'
export default {
@@ -23,7 +23,13 @@ export default {
const selected = computed({
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 }

View File

@@ -24,6 +24,8 @@ import { ref } from 'vue'
import PostEditor from '../components/PostEditor.vue'
import CategorySelect from '../components/CategorySelect.vue'
import TagSelect from '../components/TagSelect.vue'
import { API_BASE_URL, toast } from '../main'
import { getToken } from '../utils/auth'
export default {
name: 'NewPostPageView',
@@ -33,12 +35,50 @@ export default {
const content = ref('')
const selectedCategory = ref('')
const selectedTags = ref([])
const submitPost = () => {
console.log('title:', title.value)
console.log('content:', content.value)
console.log('category:', selectedCategory.value)
console.log('tags:', selectedTags.value)
// 在此处可以调用接口提交帖子
const submitPost = async () => {
if (!title.value.trim()) {
toast.error('标题不能为空')
return
}
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 }
}

View File

@@ -53,6 +53,9 @@ public class PostService {
if (tagIds == null || tagIds.isEmpty()) {
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)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
Category category = categoryRepository.findById(categoryId)