Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.ippon.belt.web.rest;
import com.ippon.belt.domain.Blog;
import com.ippon.belt.repository.BlogRepository;
import com.ippon.belt.web.rest.errors.BadRequestAlertException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.ResponseUtil;
/**
* REST controller for managing {@link com.ippon.belt.domain.Blog}.
*/
@RestController
@RequestMapping("/api")
@Transactional
public class BlogResource {
private final Logger log = LoggerFactory.getLogger(BlogResource.class);
private static final String ENTITY_NAME = "blog";
@Value("${jhipster.clientApp.name}")
private String applicationName;
private final BlogRepository blogRepository;
public BlogResource(BlogRepository blogRepository) {
this.blogRepository = blogRepository;
}
/**
* {@code POST /blogs} : Create a new blog.
*
* @param blog the blog to create.
* @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new blog, or with status {@code 400 (Bad Request)} if the blog has already an ID.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PostMapping("/blogs")
public ResponseEntity<Blog> createBlog(@Valid @RequestBody Blog blog) throws URISyntaxException {
log.debug("REST request to save Blog : {}", blog);
if (blog.getId() != null) {
throw new BadRequestAlertException("A new blog cannot already have an ID", ENTITY_NAME, "idexists");
}
Blog result = blogRepository.save(blog);
return ResponseEntity
.created(new URI("/api/blogs/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
.body(result);
}
/**
* {@code PUT /blogs/:id} : Updates an existing blog.
*
* @param id the id of the blog to save.
* @param blog the blog to update.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated blog,
* or with status {@code 400 (Bad Request)} if the blog is not valid,
* or with status {@code 500 (Internal Server Error)} if the blog couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PutMapping("/blogs/{id}")
public ResponseEntity<Blog> updateBlog(@PathVariable(value = "id", required = false) final Long id, @Valid @RequestBody Blog blog)
throws URISyntaxException {
log.debug("REST request to update Blog : {}, {}", id, blog);
if (blog.getId() == null) {
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
}
if (!Objects.equals(id, blog.getId())) {
throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
}
if (!blogRepository.existsById(id)) {
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
}
Blog result = blogRepository.save(blog);
return ResponseEntity
.ok()
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, blog.getId().toString()))
.body(result);
}
/**
* {@code PATCH /blogs/:id} : Partial updates given fields of an existing blog, field will ignore if it is null
*
* @param id the id of the blog to save.
* @param blog the blog to update.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated blog,
* or with status {@code 400 (Bad Request)} if the blog is not valid,
* or with status {@code 404 (Not Found)} if the blog is not found,
* or with status {@code 500 (Internal Server Error)} if the blog couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PatchMapping(value = "/blogs/{id}", consumes = { "application/json", "application/merge-patch+json" })
public ResponseEntity<Blog> partialUpdateBlog(
@PathVariable(value = "id", required = false) final Long id,
@NotNull @RequestBody Blog blog
) throws URISyntaxException {
log.debug("REST request to partial update Blog partially : {}, {}", id, blog);
if (blog.getId() == null) {
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
}
if (!Objects.equals(id, blog.getId())) {
throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
}
if (!blogRepository.existsById(id)) {
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
}
Optional<Blog> result = blogRepository
.findById(blog.getId())
.map(existingBlog -> {
if (blog.getName() != null) {
existingBlog.setName(blog.getName());
}
if (blog.getHandle() != null) {
existingBlog.setHandle(blog.getHandle());
}
return existingBlog;
})
.map(blogRepository::save);
return ResponseUtil.wrapOrNotFound(
result,
HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, blog.getId().toString())
);
}
/**
* {@code GET /blogs} : get all the blogs.
*
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of blogs in body.
*/
@GetMapping("/blogs")
public List<Blog> getAllBlogs() {
log.debug("REST request to get all Blogs");
return blogRepository.findByUserIsCurrentUser();
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
}
/**
* {@code GET /blogs/:id} : get the "id" blog.
*
* @param id the id of the blog to retrieve.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the blog, or with status {@code 404 (Not Found)}.
*/
@GetMapping("/blogs/{id}")
public ResponseEntity<Blog> getBlog(@PathVariable Long id) {
log.debug("REST request to get Blog : {}", id);
Optional<Blog> blog = blogRepository.findById(id);
return ResponseUtil.wrapOrNotFound(blog);
}
/**
* {@code DELETE /blogs/:id} : delete the "id" blog.
*
* @param id the id of the blog to delete.
* @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
*/
@DeleteMapping("/blogs/{id}")
public ResponseEntity<Void> deleteBlog(@PathVariable Long id) {
log.debug("REST request to delete Blog : {}", id);
blogRepository.deleteById(id);
return ResponseEntity
.noContent()
.headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()))
.build();
}
}