提交 c3cabb35 编写于 作者: E Evan

Update: 更新后端 API

上级 7a7f4016
......@@ -58,13 +58,9 @@
methods: {
loadBooks () {
var _this = this
this.$axios.get('/library').then(resp => {
this.$axios.get('/books').then(resp => {
if (resp && resp.status === 200) {
_this.books = resp.data
var length = resp.data.length
_this.cardLoading = Array.apply(null, Array(length)).map(function (item, i) {
return false
})
}
})
},
......
......@@ -70,7 +70,7 @@
onSubmit () {
console.log('submit!')
this.$axios
.post('/library', {
.post('/books', {
id: this.form.id,
cover: this.form.cover,
title: this.form.title,
......
<template>
<el-container>
<el-aside style="width: 200px;margin-top: 20px">
<SideMenu></SideMenu>
<SideMenu @indexSelect="listByCategory" ref="sideMenu"></SideMenu>
</el-aside>
<el-main>
<el-row>
<books class="books-area"></books>
<books class="books-area" ref="booksArea"></books>
</el-row>
</el-main>
</el-container>
</template>
<script>
import SideMenu from './SideMenu'
import Tag from './Tag'
import Pagination from './Pagination'
import Books from './Books'
export default {
name: 'AppLibrary',
components: {Books, Pagination, Tag, SideMenu}
import SideMenu from './SideMenu'
import Tag from './Tag'
import Pagination from './Pagination'
import Books from './Books'
export default {
name: 'AppLibrary',
components: {Books, Pagination, Tag, SideMenu},
methods: {
listByCategory () {
var _this = this
var cid = this.$refs.sideMenu.cid
var url = 'categories/' + cid + '/books'
this.$axios.get(url).then(resp => {
if (resp && resp.status === 200) {
_this.$refs.booksArea.books = resp.data
}
})
}
}
}
</script>
<style scoped>
......
......@@ -2,30 +2,33 @@
<el-menu
default-active="2"
class="el-menu-vertical-demo"
@open="handleOpen"
@close="handleClose"
@select="handleSelect"
background-color="#545c64"
text-color="#fff"
active-text-color="#ffd04b">
<el-submenu index="1">
<template slot="title">
<i class="el-icon-location"></i>
<span>分类一</span>
</template>
<el-menu-item index="1-1">子类1</el-menu-item>
<el-menu-item index="1-2">子类2</el-menu-item>
</el-submenu>
<el-menu-item index="1">
<i class="el-icon-menu"></i>
<span slot="title">文学</span>
</el-menu-item>
<el-menu-item index="2">
<i class="el-icon-menu"></i>
<span slot="title">分类二</span>
<span slot="title">流行</span>
</el-menu-item>
<el-menu-item index="3">
<i class="el-icon-document"></i>
<span slot="title">分类三</span>
<i class="el-icon-menu"></i>
<span slot="title">文化</span>
</el-menu-item>
<el-menu-item index="4">
<i class="el-icon-setting"></i>
<span slot="title">分类四</span>
<i class="el-icon-menu"></i>
<span slot="title">生活</span>
</el-menu-item>
<el-menu-item index="5">
<i class="el-icon-menu"></i>
<span slot="title">经管</span>
</el-menu-item>
<el-menu-item index="6">
<i class="el-icon-menu"></i>
<span slot="title">科技</span>
</el-menu-item>
</el-menu>
</template>
......@@ -33,12 +36,19 @@
<script>
export default {
name: 'SideMenu',
data () {
return {
cid: ''
}
},
methods: {
handleOpen (key, keyPath) {
console.log(key, keyPath)
},
handleClose (key, keyPath) {
// handleOpen (key, keyPath) {
// console.log(key, keyPath)
// },
handleSelect (key, keyPath) {
console.log(key, keyPath)
this.cid = key
this.$emit('indexSelect')
}
}
}
......
......@@ -14,13 +14,13 @@ public class LibraryController {
BookService bookService;
@CrossOrigin
@GetMapping(value = "/api/library")
@GetMapping(value = "/api/books")
public List<Book> list() throws Exception {
return bookService.list();
}
@CrossOrigin
@PostMapping(value = "/api/library")
@PostMapping(value = "/api/books")
public Book addOrUpdate(@RequestBody Book book) throws Exception {
bookService.addOrUpdate(book);
return book;
......@@ -42,4 +42,11 @@ public class LibraryController {
return bookService.Search(s.getKeywords());
}
}
@CrossOrigin
@GetMapping("/categories/{cid}/books")
public List<Book> listByCategory (@PathVariable("cid")int cid) throws Exception {
System.out.println("test");
return bookService.listByCategory(cid);
}
}
package com.gm.wj.dao;
import com.gm.wj.pojo.Book;
import com.gm.wj.pojo.Category;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface BookDAO extends JpaRepository<Book,Integer> {
List<Book> findAllByCategory(Category category);
List<Book> findAllByTitleLikeOrAuthorLike(String keyword1, String keyword2);
}
package com.gm.wj.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.gm.wj.pojo.Category;
public interface CategoryDAO extends JpaRepository<Category, Integer> {
}
......@@ -13,6 +13,10 @@ public class Book {
@Column(name = "id")
int id;
@ManyToOne
@JoinColumn(name="cid")
private Category category;
String cover;
String title;
String author;
......@@ -20,6 +24,14 @@ public class Book {
String press;
String abs;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public String getDate() {
return date;
}
......
package com.gm.wj.pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name = "category")
@JsonIgnoreProperties({ "handler","hibernateLazyInitializer" })
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
int id;
String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
......@@ -2,6 +2,7 @@ package com.gm.wj.service;
import com.gm.wj.dao.BookDAO;
import com.gm.wj.pojo.Book;
import com.gm.wj.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
......@@ -12,6 +13,8 @@ import java.util.List;
public class BookService {
@Autowired
BookDAO bookDAO;
@Autowired
CategoryService categoryService;
public List<Book> list() {
Sort sort = new Sort(Sort.Direction.DESC, "id");
......@@ -26,6 +29,12 @@ public class BookService {
bookDAO.deleteById(id);
}
public List<Book> listByCategory(int cid) {
Category category = categoryService.get(cid);
Sort sort = new Sort(Sort.Direction.DESC, "id");
return bookDAO.findAllByCategory(category);
}
public List<Book> Search(String keywords) {
return bookDAO.findAllByTitleLikeOrAuthorLike('%' + keywords + '%', '%' + keywords + '%');
}
......
package com.gm.wj.service;
import com.gm.wj.dao.CategoryDAO;
import com.gm.wj.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CategoryService {
@Autowired
CategoryDAO categoryDAO;
public List<Category> list() {
Sort sort = new Sort(Sort.Direction.DESC, "id");
return categoryDAO.findAll(sort);
}
public Category get(int id) {
Category c= categoryDAO.findById(id).orElse(null);
return c;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册