|
| 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 | +} |
0 commit comments