-
Notifications
You must be signed in to change notification settings - Fork 1
/
PetitDejResource.java
185 lines (166 loc) · 7.89 KB
/
PetitDejResource.java
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
151
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
183
184
185
package com.dooapp.petitdej.web.rest;
import com.dooapp.petitdej.domain.PetitDej;
import com.dooapp.petitdej.repository.PetitDejRepository;
import com.dooapp.petitdej.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.dooapp.petitdej.domain.PetitDej}.
*/
@RestController
@RequestMapping("/api")
@Transactional
public class PetitDejResource {
private final Logger log = LoggerFactory.getLogger(PetitDejResource.class);
private static final String ENTITY_NAME = "petitDej";
@Value("${jhipster.clientApp.name}")
private String applicationName;
private final PetitDejRepository petitDejRepository;
public PetitDejResource(PetitDejRepository petitDejRepository) {
this.petitDejRepository = petitDejRepository;
}
/**
* {@code POST /petit-dejs} : Create a new petitDej.
*
* @param petitDej the petitDej to create.
* @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new petitDej, or with status {@code 400 (Bad Request)} if the petitDej has already an ID.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PostMapping("/petit-dejs")
public ResponseEntity<PetitDej> createPetitDej(@Valid @RequestBody PetitDej petitDej) throws URISyntaxException {
log.debug("REST request to save PetitDej : {}", petitDej);
if (petitDej.getId() != null) {
throw new BadRequestAlertException("A new petitDej cannot already have an ID", ENTITY_NAME, "idexists");
}
PetitDej result = petitDejRepository.save(petitDej);
return ResponseEntity
.created(new URI("/api/petit-dejs/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
.body(result);
}
/**
* {@code PUT /petit-dejs/:id} : Updates an existing petitDej.
*
* @param id the id of the petitDej to save.
* @param petitDej the petitDej to update.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated petitDej,
* or with status {@code 400 (Bad Request)} if the petitDej is not valid,
* or with status {@code 500 (Internal Server Error)} if the petitDej couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PutMapping("/petit-dejs/{id}")
public ResponseEntity<PetitDej> updatePetitDej(
@PathVariable(value = "id", required = false) final Long id,
@Valid @RequestBody PetitDej petitDej
) throws URISyntaxException {
log.debug("REST request to update PetitDej : {}, {}", id, petitDej);
if (petitDej.getId() == null) {
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
}
if (!Objects.equals(id, petitDej.getId())) {
throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
}
if (!petitDejRepository.existsById(id)) {
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
}
PetitDej result = petitDejRepository.save(petitDej);
return ResponseEntity
.ok()
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, petitDej.getId().toString()))
.body(result);
}
/**
* {@code PATCH /petit-dejs/:id} : Partial updates given fields of an existing petitDej, field will ignore if it is null
*
* @param id the id of the petitDej to save.
* @param petitDej the petitDej to update.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated petitDej,
* or with status {@code 400 (Bad Request)} if the petitDej is not valid,
* or with status {@code 404 (Not Found)} if the petitDej is not found,
* or with status {@code 500 (Internal Server Error)} if the petitDej couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PatchMapping(value = "/petit-dejs/{id}", consumes = { "application/json", "application/merge-patch+json" })
public ResponseEntity<PetitDej> partialUpdatePetitDej(
@PathVariable(value = "id", required = false) final Long id,
@NotNull @RequestBody PetitDej petitDej
) throws URISyntaxException {
log.debug("REST request to partial update PetitDej partially : {}, {}", id, petitDej);
if (petitDej.getId() == null) {
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
}
if (!Objects.equals(id, petitDej.getId())) {
throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
}
if (!petitDejRepository.existsById(id)) {
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
}
Optional<PetitDej> result = petitDejRepository
.findById(petitDej.getId())
.map(existingPetitDej -> {
if (petitDej.getDate() != null) {
existingPetitDej.setDate(petitDej.getDate());
}
if (petitDej.getCommentaire() != null) {
existingPetitDej.setCommentaire(petitDej.getCommentaire());
}
return existingPetitDej;
})
.map(petitDejRepository::save);
return ResponseUtil.wrapOrNotFound(
result,
HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, petitDej.getId().toString())
);
}
/**
* {@code GET /petit-dejs} : get all the petitDejs.
*
* @param eagerload flag to eager load entities from relationships (This is applicable for many-to-many).
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of petitDejs in body.
*/
@GetMapping("/petit-dejs")
public List<PetitDej> getAllPetitDejs(@RequestParam(required = false, defaultValue = "false") boolean eagerload) {
log.debug("REST request to get all PetitDejs");
return petitDejRepository.findAllWithEagerRelationships();
}
/**
* {@code GET /petit-dejs/:id} : get the "id" petitDej.
*
* @param id the id of the petitDej to retrieve.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the petitDej, or with status {@code 404 (Not Found)}.
*/
@GetMapping("/petit-dejs/{id}")
public ResponseEntity<PetitDej> getPetitDej(@PathVariable Long id) {
log.debug("REST request to get PetitDej : {}", id);
Optional<PetitDej> petitDej = petitDejRepository.findOneWithEagerRelationships(id);
return ResponseUtil.wrapOrNotFound(petitDej);
}
/**
* {@code DELETE /petit-dejs/:id} : delete the "id" petitDej.
*
* @param id the id of the petitDej to delete.
* @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
*/
@DeleteMapping("/petit-dejs/{id}")
public ResponseEntity<Void> deletePetitDej(@PathVariable Long id) {
log.debug("REST request to delete PetitDej : {}", id);
petitDejRepository.deleteById(id);
return ResponseEntity
.noContent()
.headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()))
.build();
}
}