Skip to content

Commit 9c00a97

Browse files
committed
link
1 parent b523663 commit 9c00a97

7 files changed

Lines changed: 339 additions & 2 deletions

File tree

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package info.xiaomo.application.controller;
2+
3+
import info.xiaomo.application.model.LinkModel;
4+
import info.xiaomo.application.service.LinkService;
5+
import info.xiaomo.core.constant.Err;
6+
import info.xiaomo.core.controller.BaseController;
7+
import info.xiaomo.core.controller.Result;
8+
import io.swagger.annotations.Api;
9+
import io.swagger.annotations.ApiImplicitParam;
10+
import io.swagger.annotations.ApiImplicitParams;
11+
import io.swagger.annotations.ApiOperation;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.http.MediaType;
14+
import org.springframework.web.bind.annotation.*;
15+
16+
import java.util.List;
17+
18+
/**
19+
* 把今天最好的表现当作明天最新的起点..~
20+
* いま 最高の表現 として 明日最新の始発..~
21+
* Today the best performance as tomorrow newest starter!
22+
* Created by IntelliJ IDEA.
23+
*
24+
* @author: xiaomo
25+
* @github: https://github.com/qq83387856
26+
* @email: hupengbest@163.com
27+
* @QQ_NO: 83387856
28+
* @Date: 2016/4/1119:55
29+
* @Description: 友情连接控制器
30+
* @Copyright(©) 2015 by xiaomo.
31+
**/
32+
@RestController
33+
@RequestMapping("/link")
34+
@Api(value = "LinkController",description = "友情链接相关api")
35+
public class LinkController extends BaseController {
36+
37+
private final LinkService service;
38+
39+
@Autowired
40+
public LinkController(LinkService service) {
41+
this.service = service;
42+
}
43+
44+
/**
45+
* 根据id查找
46+
*
47+
* @param id id
48+
* @return model
49+
*/
50+
@RequestMapping(value = "findById/{id}",method = RequestMethod.GET)
51+
@ApiOperation(value = "通过id查找", notes = "通过id查找",httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
52+
@ApiImplicitParams({
53+
@ApiImplicitParam(name = "id", value = "唯一id", required = true, dataType = "Long", paramType = "path")
54+
})
55+
public Result findLinkById(@PathVariable("id") Long id) {
56+
LinkModel model = service.findById(id);
57+
if (model == null) {
58+
return new Result(Err.NULL_DATA.getResultCode(), Err.NULL_DATA.getMessage());
59+
}
60+
return new Result(model);
61+
}
62+
63+
/**
64+
* 根据名字查找
65+
*
66+
* @param name name
67+
* @return model
68+
*/
69+
@RequestMapping(value = "findByName/{name}",method = RequestMethod.GET)
70+
@ApiOperation(value = "根据名字查找", notes = "根据名字查找",httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
71+
@ApiImplicitParams({
72+
@ApiImplicitParam(name = "name", value = "友情链接名字", required = true, dataType = "String", paramType = "path")
73+
})
74+
public Result findByName(@PathVariable("name") String name) {
75+
LinkModel model = service.findByName(name);
76+
if (model == null) {
77+
return new Result(Err.NULL_DATA.getResultCode(), Err.NULL_DATA.getMessage());
78+
}
79+
return new Result(model);
80+
}
81+
82+
83+
/**
84+
* 返回所有数据
85+
*
86+
* @return 所有
87+
*/
88+
@RequestMapping(value = "findAll",method = RequestMethod.GET)
89+
@ApiOperation(value = "返回所有数据", notes = "返回所有数据",httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
90+
public Result findAll() {
91+
List<LinkModel> pages = service.findAll();
92+
if (pages == null || pages.size() == 0) {
93+
return new Result(Err.NULL_DATA.getResultCode(), Err.NULL_DATA.getMessage());
94+
}
95+
return new Result(pages);
96+
}
97+
98+
99+
/**
100+
* 添加链接
101+
*
102+
* @return model
103+
*/
104+
@RequestMapping(value = "add",method = RequestMethod.POST)
105+
@ApiOperation(value = "添加链接", notes = "添加链接",httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
106+
public Result add(@RequestBody LinkModel model) {
107+
LinkModel linkModel = service.findByName(model.getName());
108+
if (linkModel != null) {
109+
return new Result(Err.REPEAT.getResultCode(), Err.REPEAT.getMessage());
110+
}
111+
linkModel = new LinkModel();
112+
linkModel.setName(model.getName());
113+
linkModel.setPosition(model.getPosition());
114+
linkModel.setUrl(model.getUrl());
115+
LinkModel addModel = service.add(linkModel);
116+
return new Result(addModel);
117+
}
118+
119+
/**
120+
* 更新链接
121+
*
122+
* @return model
123+
*/
124+
@ApiOperation(value = "更新链接", notes = "更新链接",httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
125+
@RequestMapping(value = "update",method = RequestMethod.POST)
126+
public Result update(@RequestBody LinkModel model) {
127+
LinkModel linkModel = service.findById(model.getId());
128+
if (linkModel == null) {
129+
return new Result(Err.NULL_DATA.getResultCode(), Err.NULL_DATA.getMessage());
130+
}
131+
linkModel.setName(model.getName());
132+
linkModel.setUrl(model.getUrl());
133+
linkModel.setPosition(model.getPosition());
134+
LinkModel updateModel = service.update(linkModel);
135+
return new Result(updateModel);
136+
}
137+
138+
/**
139+
* 删除链接
140+
*
141+
* @param id id
142+
* @return model
143+
*/
144+
@RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
145+
@ApiOperation(value = "删除链接", notes = "删除链接",httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
146+
@ApiImplicitParams({
147+
@ApiImplicitParam(name = "id", value = "唯一id", required = true, dataType = "Long", paramType = "path")
148+
})
149+
public Result delete(@PathVariable("id") Long id) {
150+
LinkModel LinkModel = service.findById(id);
151+
if (LinkModel == null) {
152+
return new Result(Err.NULL_DATA.getResultCode(), Err.NULL_DATA.getMessage());
153+
}
154+
LinkModel delModel = service.delete(id);
155+
return new Result(delModel);
156+
}
157+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package info.xiaomo.application.dao;
2+
3+
import info.xiaomo.application.model.LinkModel;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
/**
8+
* 把今天最好的表现当作明天最新的起点..~
9+
* いま 最高の表現 として 明日最新の始発..~
10+
* Today the best performance as tomorrow newest starter!
11+
* Created by IntelliJ IDEA.
12+
*
13+
* @author: xiaomo
14+
* @github: https://github.com/qq83387856
15+
* @email: hupengbest@163.com
16+
* @QQ_NO: 83387856
17+
* @Date: 2016/4/1119:52
18+
* @Copyright(©) 2015 by xiaomo.
19+
**/
20+
@Repository
21+
public interface LinkDao extends JpaRepository<LinkModel, Long> {
22+
23+
LinkModel findLinkByName(String name);
24+
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package info.xiaomo.application.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import info.xiaomo.application.model.base.BaseModel;
5+
import io.swagger.annotations.ApiModel;
6+
import lombok.*;
7+
8+
import javax.persistence.Entity;
9+
import javax.persistence.Table;
10+
11+
/**
12+
* @author 小莫 (https://xiaomo.info) (https://github.com/syoubaku)
13+
* @created : 2016/12/17 15:08
14+
*/
15+
@Entity
16+
@Table(name = "link")
17+
@Data
18+
@ToString(callSuper = true)
19+
@EqualsAndHashCode(callSuper = false)
20+
@AllArgsConstructor
21+
@NoArgsConstructor
22+
@ApiModel(value = "友链实体类")
23+
public class LinkModel extends BaseModel {
24+
25+
private String name;
26+
27+
private String url;
28+
29+
private int position;
30+
31+
}

application/src/main/java/info/xiaomo/application/model/UserModel.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
@EqualsAndHashCode(callSuper = false)
3333
@AllArgsConstructor
3434
@NoArgsConstructor
35-
@JsonInclude(JsonInclude.Include.NON_EMPTY)
3635
@ApiModel(value = "用户实体类")
3736
public class UserModel extends BaseModel implements Serializable {
3837

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package info.xiaomo.application.service;
2+
3+
4+
import info.xiaomo.application.model.LinkModel;
5+
import org.springframework.data.domain.Page;
6+
7+
import java.util.List;
8+
9+
/**
10+
* 把今天最好的表现当作明天最新的起点..~
11+
* いま 最高の表現 として 明日最新の始発..~
12+
* Today the best performance as tomorrow newest starter!
13+
* Created by IntelliJ IDEA.
14+
*
15+
* @author: xiaomo
16+
* @github: https://github.com/qq83387856
17+
* @email: hupengbest@163.com
18+
* @QQ_NO: 83387856
19+
* @Date: 2016/4/1119:49
20+
* @Copyright(©) 2015 by xiaomo.
21+
**/
22+
public interface LinkService {
23+
24+
LinkModel findById(Long id);
25+
26+
LinkModel findByName(String name);
27+
28+
Page<LinkModel> findAll(int start, int pageSize);
29+
30+
List<LinkModel> findAll();
31+
32+
LinkModel add(LinkModel model);
33+
34+
LinkModel update(LinkModel model);
35+
36+
LinkModel delete(Long id);
37+
38+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package info.xiaomo.application.service.impl;
2+
3+
import info.xiaomo.application.dao.LinkDao;
4+
import info.xiaomo.application.model.LinkModel;
5+
import info.xiaomo.application.service.LinkService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.data.domain.Page;
8+
import org.springframework.data.domain.PageRequest;
9+
import org.springframework.data.domain.Sort;
10+
import org.springframework.stereotype.Service;
11+
12+
import java.util.List;
13+
14+
/**
15+
* 把今天最好的表现当作明天最新的起点..~
16+
* いま 最高の表現 として 明日最新の始発..~
17+
* Today the best performance as tomorrow newest starter!
18+
* Created by IntelliJ IDEA.
19+
*
20+
* @author: xiaomo
21+
* @github: https://github.com/qq83387856
22+
* @email: hupengbest@163.com
23+
* @QQ_NO: 83387856
24+
* @Date: 2016/4/11 19:50
25+
* @Copyright(©) 2015 by xiaomo.
26+
**/
27+
@Service
28+
public class LinkServiceImpl implements LinkService {
29+
30+
private final LinkDao dao;
31+
32+
@Autowired
33+
public LinkServiceImpl(LinkDao dao) {
34+
this.dao = dao;
35+
}
36+
37+
@Override
38+
public LinkModel findById(Long id) {
39+
return dao.findOne(id);
40+
}
41+
42+
@Override
43+
public LinkModel findByName(String name) {
44+
return dao.findLinkByName(name);
45+
}
46+
47+
@Override
48+
public Page<LinkModel> findAll(int start, int pageSize) {
49+
Sort sort = new Sort(Sort.Direction.DESC, "order");
50+
return dao.findAll(new PageRequest(start - 1, pageSize, sort));
51+
}
52+
53+
@Override
54+
public List<LinkModel> findAll() {
55+
return dao.findAll();
56+
}
57+
58+
@Override
59+
public LinkModel add(LinkModel model) {
60+
return dao.save(model);
61+
}
62+
63+
@Override
64+
public LinkModel update(LinkModel model) {
65+
LinkModel updateModel = dao.findOne(model.getId());
66+
if (model.getName() != null) {
67+
updateModel.setName(model.getName());
68+
}
69+
if (model.getUrl() != null) {
70+
updateModel.setUrl(model.getUrl());
71+
}
72+
if (model.getPosition() > 0) {
73+
updateModel.setPosition(model.getPosition());
74+
}
75+
return dao.save(updateModel);
76+
}
77+
78+
@Override
79+
public LinkModel delete(Long id) {
80+
LinkModel model = dao.findOne(id);
81+
if (model != null) {
82+
dao.delete(id);
83+
}
84+
return model;
85+
}
86+
}

core/src/main/java/info/xiaomo/core/constant/Err.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* @Copyright(©) 2015 by xiaomo.
1616
*/
1717
public enum Err {
18-
SUCCESS(0, "成功"),
18+
SUCCESS(200, "成功"),
19+
NOT_FOUNT(404, "找不到"),
1920
REPEAT(992, "数据重复"),
2021
ERROR(993, "系统错误"),
2122
ADMIN_USER_REPEAT(994, "后台用户名重复"),

0 commit comments

Comments
 (0)