Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target/
.classpath
.project
.settings
ansible/vaults/*
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
# SpringRestPOC
to run locally:

## Setup a database locally

mvn clean install
```bash
sudo yum install -y postgresql-server
sudo /usr/bin/postgresql-setup initdb
sudo systemctl enable postgresql
sudo systemctl start postgresql
sudo systemctl status postgresql
psql
create user myuser password '...';
create database mce owner myuser;
\q
psql mce
create table hat(id serial primary key, type text, color text, description text);
alter table hat owner to myuser;
\q
```

# Run SpringRestPOC locally

mvn spring-boot:run
```bash
mvn clean install
SPRING_DATASOURCE_USERNAME=myuser SPRING_DATASOURCE_PASSWORD=... SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/mce mvn spring-boot:run
```

This requires java 8 or higher

# Test the SpringRESTPOC API

curl http://localhost:8080/hats; echo
curl -X POST -H "Content-Type: application/json" -d '{ "color":"Red", "type":"Fedora" }' http://localhost:8080/hats; echo
curl -X POST -H "Content-Type: application/json" -d '{ "color":"White", "type":"White Sox Hat" }' http://localhost:8080/hats; echo
28 changes: 24 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-data-jpa</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
Expand All @@ -49,6 +57,18 @@
</exclusion>
</exclusions>
</dependency>

<!-- Tests -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

2 changes: 2 additions & 0 deletions src/main/java/com/redhat/poc/SpringRestPocApplication.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.redhat.poc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAutoConfiguration
public class SpringRestPocApplication {

public static void main(String[] args) {
Expand Down
27 changes: 6 additions & 21 deletions src/main/java/com/redhat/poc/dao/HatDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,12 @@

import com.redhat.poc.model.Hat;

// put logic get connection and query db in here
public class HatDAO {

public Hat createHat(Hat hat){
return new Hat();
}


public Hat getHat(Integer id){

import java.util.Optional;

return new Hat();
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

}

public void deleteHat(Integer id){
}

public Hat updateHat(Hat hat){


return hat;
}
// put logic get connection and query db in here
@Repository
public interface HatDAO extends CrudRepository<Hat, Integer> {
}
22 changes: 17 additions & 5 deletions src/main/java/com/redhat/poc/model/Hat.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package com.redhat.poc.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

@Entity
@Table(name = "hat")
public class Hat {

String color;
String type;
String descrption;
String description;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Integer id;

public String getColor() {
Expand All @@ -23,12 +35,12 @@ public void setType(String type) {
this.type = type;
}

public String getDescrption() {
return descrption;
public String getDescription() {
return description;
}

public void setDescrption(String descrption) {
this.descrption = descrption;
public void setDescription(String descrption) {
this.description = descrption;
}

public Integer getId() {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/redhat/poc/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.redhat.poc.model.Hat;
import com.redhat.poc.service.HatService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -17,7 +19,7 @@
@org.springframework.web.bind.annotation.RestController
public class RestController {

// put logic to retrieve, update, create, delete the hats in hat service. Currently below is just a place holder that need to be replaced
@Autowired
private HatService hatService;

@GetMapping("/test")
Expand All @@ -31,8 +33,8 @@ public Hat getHat(@PathVariable("hatId") Integer hatId){
}

@PostMapping("/hats")
public ResponseEntity createHat(@RequestBody Hat hat){
return new ResponseEntity( hat, HttpStatus.CREATED );
public ResponseEntity<Hat> createHat(@RequestBody Hat hat){
return new ResponseEntity<>( hatService.createHat(hat), HttpStatus.CREATED );
}

@PutMapping("/hats/{hatId}")
Expand All @@ -42,14 +44,13 @@ public Hat updateHat(@RequestBody Hat hat, @PathVariable("hatId") Integer hatId
}

@DeleteMapping("/hats/{hatId}")
public ResponseEntity updateHat(@PathVariable("hatId") Integer hatId) {
public ResponseEntity<?> deleteHat(@PathVariable("hatId") Integer hatId) {
return new ResponseEntity<Void>( HttpStatus.NO_CONTENT );
}

@GetMapping("/hats")
public List<Hat> getAllHats(){
List<Hat> hats = new ArrayList<>();
return hats;
public Iterable<Hat> getAllHats(){
return hatService.getHats();
}

}
31 changes: 20 additions & 11 deletions src/main/java/com/redhat/poc/service/HatService.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
package com.redhat.poc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.redhat.poc.dao.HatDAO;
import com.redhat.poc.model.Hat;

@Component
public class HatService {
// put logic to query the db in hatDAO

@Autowired
private HatDAO dao;

public Hat createHat(Hat hat){
return dao.createHat(hat);
// return dao.save(hat);
Iterable<Hat> a = dao.findAll();
System.out.println(a);
Hat o = dao.save(hat);
a = dao.findAll();
System.out.println(a);
return o;
}


public Iterable<Hat> getHats(){
return dao.findAll();
}

public Hat getHat(Integer id){
Hat hat = new Hat();
hat.setId(id);
hat.setColor("blue");
hat.setType("fedora");
hat.setDescrption("blue hat");
// return dao.getHat(id);
return hat;
return dao.findById(id).orElse(null);
}

public void deleteHat(Integer id){
dao.deleteHat(id);
dao.deleteById(id);
}

public Hat updateHat(Hat hat){
return dao.updateHat(hat);
return dao.save(hat);
}


Expand Down
Loading