Skip to content

Commit a359905

Browse files
committed
Spring Boot RESTful API
1 parent 05caee0 commit a359905

File tree

7 files changed

+22
-41
lines changed

7 files changed

+22
-41
lines changed

springboot-examples/springboot-mockmvc-hsql/src/main/java/com/hellokoding/springboot/restful/StockAPI.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import lombok.RequiredArgsConstructor;
44
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.http.HttpStatus;
56
import org.springframework.http.ResponseEntity;
7+
import org.springframework.util.StringUtils;
68
import org.springframework.web.bind.annotation.*;
79

810
import java.util.List;
11+
import java.util.Objects;
912
import java.util.Optional;
1013

1114
@RequiredArgsConstructor
@@ -34,7 +37,7 @@ public ResponseEntity<Stock> findById(@PathVariable Long stockId) {
3437

3538
@PostMapping
3639
public ResponseEntity create(@RequestBody Stock stock) {
37-
return ResponseEntity.ok(stockService.save(stock));
40+
return ResponseEntity.status(HttpStatus.CREATED).body(stockService.save(stock));
3841
}
3942

4043
@PatchMapping("/{stockId}")
@@ -46,9 +49,16 @@ public ResponseEntity<Stock> update(@PathVariable Long stockId, @RequestBody Sto
4649
}
4750

4851
Stock stock = stockOptional.get();
49-
if (updatingStock.getName() != null) stock.setName(updatingStock.getName());
50-
if (updatingStock.getPrice() != null) stock.setPrice(updatingStock.getPrice());
52+
if (!StringUtils.isEmpty(updatingStock.getName())) stock.setName(updatingStock.getName());
53+
if (!Objects.isNull(updatingStock.getPrice())) stock.setPrice(updatingStock.getPrice());
5154

52-
return ResponseEntity.ok(stockService.save(stock));
55+
return ResponseEntity.accepted().body(stockService.save(stock));
56+
}
57+
58+
@DeleteMapping("/{id}")
59+
public ResponseEntity delete(@PathVariable Long id) {
60+
stockService.deleteById(id);
61+
62+
return ResponseEntity.accepted().build();
5363
}
5464
}

springboot-examples/springboot-mockmvc-hsql/src/main/java/com/hellokoding/springboot/restful/StockService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ public Optional<Stock> findById(Long id) {
2323
public Stock save(Stock stock) {
2424
return stockRepository.save(stock);
2525
}
26+
27+
public void deleteById(Long id) {
28+
stockRepository.deleteById(id);
29+
}
2630
}

springboot-examples/springboot-mockmvc-hsql/src/test/java/com/hellokoding/springboot/restful/StockAPITest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
@RunWith(SpringRunner.class)
2121
@WebMvcTest
2222
public class StockAPITest {
23-
2423
@Autowired
2524
private MockMvc mockMvc;
2625

@@ -29,6 +28,7 @@ public class StockAPITest {
2928

3029
@Test
3130
public void findAll() throws Exception {
31+
// given
3232
Stock stock = new Stock();
3333
stock.setId(1L);
3434
stock.setName("Stock 1");
@@ -37,6 +37,7 @@ public void findAll() throws Exception {
3737
List<Stock> stocks = Arrays.asList(stock);
3838
given(stockService.findAll()).willReturn(stocks);
3939

40+
// when + then
4041
this.mockMvc.perform(get("/api/v1/stocks"))
4142
.andExpect(status().isOk())
4243
.andExpect(content().json("[{'id': 1,'name': 'Stock 1';'price': 1}]"));

springboot-examples/springboot-restfulapi/Dockerfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

springboot-examples/springboot-restfulapi/docker-compose.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

springboot-examples/springboot-restfulapi/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
<artifactId>spring-boot-starter-data-jpa</artifactId>
3131
</dependency>
3232
<dependency>
33-
<groupId>mysql</groupId>
34-
<artifactId>mysql-connector-java</artifactId>
33+
<groupId>org.hsqldb</groupId>
34+
<artifactId>hsqldb</artifactId>
3535
</dependency>
3636
</dependencies>
3737

springboot-examples/springboot-restfulapi/src/main/resources/application.properties

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)