Skip to content

Commit

Permalink
改用数据库储存内容
Browse files Browse the repository at this point in the history
  • Loading branch information
Astral-Yang committed Feb 15, 2023
1 parent b8256ab commit 0e5ebf5
Show file tree
Hide file tree
Showing 29 changed files with 110 additions and 167 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .mvn/wrapper/maven-wrapper.jar
100644 → 100755
Empty file.
Empty file modified .mvn/wrapper/maven-wrapper.properties
100644 → 100755
Empty file.
Empty file modified mvnw.cmd
100644 → 100755
Empty file.
1 change: 0 additions & 1 deletion pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down
Empty file modified src/main/java/com/astral/dao_blog/DaoBlogApplication.java
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/main/java/com/astral/dao_blog/config/WebSecurityConfig.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Except
.antMatchers("/api/**").permitAll()
.anyRequest().permitAll()
.and().headers().frameOptions().sameOrigin()
.and().csrf().disable();;
.and().csrf().disable();
return httpSecurity.build();
}

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/astral/dao_blog/controller/ArticleController.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.astral.dao_blog.entity.article;
import com.astral.dao_blog.entity.comment;
import com.astral.dao_blog.service.ArticleService;
import com.astral.dao_blog.util.articleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

Expand All @@ -19,16 +18,26 @@ public List<article> show() {
return articleService.show();
}

@GetMapping("/api/getOne")
public article getOne(@RequestParam("id") int id) {
return articleService.getOne(id);
}

@GetMapping("/api/comment")
public List<comment> comments(@RequestParam("url") String url) {
return articleService.comments(url);
public List<comment> comments(@RequestParam("id") int id) {
return articleService.comments(id);
}

@PostMapping("/admin/submit")
public String editor(@RequestBody articleInfo articleInfo){
public String editor(@RequestBody article articleInfo){
return articleService.submit(articleInfo);
}

@PatchMapping("/admin/change")
public String change(@RequestBody article articleInfo){
return articleService.change(articleInfo);
}

@DeleteMapping("/admin/delete")
public String delete(@RequestParam("id") int id) {
return articleService.delete(id);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/astral/dao_blog/controller/AssortController.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@

@RestController
public class AssortController {

final AssortService assortService;
@Autowired
AssortService assortService;
public AssortController(AssortService assortService){
this.assortService=assortService;
}
@GetMapping("/admin/allSorts")
public List<assort> allSorts(){
return assortService.allAssort();
}

@PostMapping("/admin/newSort")
public String newSort(@RequestBody String name){
return assortService.newAssort(name);
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/main/java/com/astral/dao_blog/controller/UploadController.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class UploadController {
// @RequestMapping("/uploadArticle")
// public String upload(@RequestParam("article") MultipartFile article) throws FileNotFoundException {
// String path = ResourceUtils.getURL("classpath:").getPath() + "/static/article";
// String path = ResourceUtils.getContent("classpath:").getPath() + "/static/article";
// UUID uuid = UUID.randomUUID();
// String articleName = uuid.toString();
// try {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/astral/dao_blog/dao/ArticleDao.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

import java.util.List;

public interface ArticleDao extends JpaRepository<article,Integer>, JpaSpecificationExecutor<article> {
article findByURL(String URL);
List<article> findByAsId(int id);
article findByTopic(String topic);
public interface ArticleDao extends JpaRepository<article, Integer>, JpaSpecificationExecutor<article> {
List<article> findAllByAsName(String name);

article findByTopic(String topic);

boolean existsByTopic(String topic);
}
2 changes: 2 additions & 0 deletions src/main/java/com/astral/dao_blog/dao/AssortDao.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

public interface AssortDao extends JpaRepository<assort,Integer>, JpaSpecificationExecutor<assort> {
assort findByName(String name);
boolean existsByName(String name);

}
Empty file modified src/main/java/com/astral/dao_blog/dao/CommentDao.java
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions src/main/java/com/astral/dao_blog/entity/article.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class article {
String topic;
@Column
String intro;
@Column
String URL;
@Column(columnDefinition = "LONGTEXT")
String Content;
@Column
@JsonSerialize(using = DateSerializer.class)
Date releaseDate;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/astral/dao_blog/entity/assort.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
@Data
public class assort {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;
@Column
String name;
// @Column
// Integer id;
@JsonIgnore
@Column
@OneToMany(mappedBy = "as")
Expand Down
Empty file modified src/main/java/com/astral/dao_blog/entity/comment.java
100644 → 100755
Empty file.
8 changes: 5 additions & 3 deletions src/main/java/com/astral/dao_blog/service/ArticleService.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.astral.dao_blog.entity.article;
import com.astral.dao_blog.entity.comment;
import com.astral.dao_blog.util.articleInfo;

import java.util.List;

public interface ArticleService {
public List<article> show();
public List<comment> comments(String url);
public String submit(articleInfo articleInfo);
public article getOne(int id);
public List<comment> comments(int id);
public String submit(article articleInfo);
public String change(article articleInfo);

public String delete(int id);
}
Empty file modified src/main/java/com/astral/dao_blog/service/AssortService.java
100644 → 100755
Empty file.
Empty file modified src/main/java/com/astral/dao_blog/service/CommentService.java
100644 → 100755
Empty file.
89 changes: 65 additions & 24 deletions src/main/java/com/astral/dao_blog/service/impl/ArticleServiceImpl.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,66 +1,107 @@
package com.astral.dao_blog.service.impl;

import com.astral.dao_blog.dao.ArticleDao;
import com.astral.dao_blog.dao.AssortDao;
import com.astral.dao_blog.entity.article;
import com.astral.dao_blog.entity.comment;
import com.astral.dao_blog.service.ArticleService;
import com.astral.dao_blog.util.articleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
ArticleDao articleDao;

@Autowired
AssortDao assortDao;

@Override
public List<article> show() {
return articleDao.findAll();
}

@Override
public List<comment> comments(String url) {
article art = articleDao.findByURL(url);
public article getOne(int id) {
return articleDao.findById(id).get();
}

@Override
public List<comment> comments(int id) {
article art = articleDao.findById(id).get();
return art.getComments();
}

@Override
@Transactional(rollbackFor = Exception.class)
public String submit(articleInfo articleInfo){
public String submit(article articleInfo) {
try {
try {
if (!articleDao.findByTopic(articleInfo.getForm().getTopic()).getTopic().isEmpty())
return "存在同名文章,请重试~";
}catch (NullPointerException e) {
articleDao.saveAndFlush(articleInfo.getForm());
String path = ResourceUtils.getURL("classpath:").getPath() + "/static/article";
String articleName = articleInfo.getForm().getTopic();
//保存Content为文件
byte[] contentByte = articleInfo.getContent().getBytes();
File file = new File(path + "/" + articleName + ".md");
if (!file.exists())
file.createNewFile();
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(contentByte);
outStream.close(); //关闭文件输出流
if (!assortDao.existsByName(articleInfo.getAs().getName())) {
assortDao.save(articleInfo.getAs());
}
if (articleDao.existsByTopic(articleInfo.getTopic()))
return "存在同名文章,请重试~";
articleDao.saveAndFlush(articleInfo);
// String path = ResourceUtils.getURL("classpath:").getPath() + "/static/article";
// String articleName = articleInfo.getForm().getTopic();

//判断目标文件所在的目录是否存在 防止丢失目录
// if(!file.getParentFile().exists()) {
// //如果目标文件所在的目录不存在,则创建父目录
// System.out.println("目标文件所在目录不存在,准备创建它!");
// if(!file.getParentFile().mkdirs()) {
// System.out.println("创建目标文件所在目录失败!");
// return false;
// }
// }
//保存Content为文件
// System.out.println(path);
// byte[] contentByte = articleInfo.getContent().getBytes();
// File file = new File(path + "/" + articleName + ".md");
// FileOutputStream outStream = new FileOutputStream(file);
// outStream.write(contentByte);
// outStream.close(); //关闭文件输出流
} catch (Exception e) {
return "发布失败,请重试~";
}
return "发布成功!";
}

@Override
public String change(article articleInfo) {
try {
if (!assortDao.existsByName(articleInfo.getAs().getName())) {
assortDao.save(articleInfo.getAs());
}
//修改文件内容
// String path = ResourceUtils.getURL("classpath:").getPath() + "/static/article";
// //保存Content为文件
// byte[] contentByte = articleInfo.getContent().getBytes();
// article origin = articleDao.findById(articleInfo.getForm().getId()).get();
// File file = new File(path + "/" + origin.getContent() + ".md");
// FileOutputStream outputStream = new FileOutputStream(file);
// outputStream.write(contentByte);
// Files.move(file.toPath(),file.toPath().resolveSibling(articleInfo.getForm().getContent()+".md"));
// outputStream.close(); //关闭文件输出流
articleDao.save(articleInfo);
} catch (Exception e) {
return "修改失败,请重试~";
}
return "修改成功!";
}

@Override
public String delete(int id) {
try {
articleDao.deleteById(id);
}catch (Exception e){
// article origin = articleDao.findById(id).get();
// String path = ResourceUtils.getURL("classpath:").getPath() + "/static/article";
// System.out.println(path);
// Files.delete(Paths.get(path + "/" + origin.getContent() + ".md"));
articleDao.deleteById(id);
} catch (Exception e) {
return "删除失败,请重试~";
}
return "删除成功!";
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/astral/dao_blog/service/impl/AssortServiceImpl.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ public class AssortServiceImpl implements AssortService {

@Override
public List<article> ofAssort(String name) {
return articleDao.findByAsId(assortDao.findByName(name).getId());
return articleDao.findAllByAsName(name);
}

@Override
public List<assort> allAssort() {
return assortDao.findAll();
Expand Down
Empty file.
Empty file modified src/main/java/com/astral/dao_blog/util/DateSerializer.java
100644 → 100755
Empty file.
Empty file modified src/main/java/com/astral/dao_blog/util/articleInfo.java
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ spring:
datasource:
username: root
password: M142753869.
driver-class-name: com.mysql.jdbc.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?serverTimezone=UTC&characterEncoding=UTF-8
hikari:
minimum-idle: 8
Expand Down
46 changes: 0 additions & 46 deletions src/main/resources/static/article/fir.md

This file was deleted.

Loading

0 comments on commit 0e5ebf5

Please sign in to comment.