Initial commit
This commit is contained in:
commit
e4c00f10e8
37 changed files with 2512 additions and 0 deletions
30
server/src/main/java/server/Config.java
Normal file
30
server/src/main/java/server/Config.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package server;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class Config {
|
||||
|
||||
@Bean
|
||||
public Random getRandom() {
|
||||
return new Random();
|
||||
}
|
||||
}
|
||||
29
server/src/main/java/server/Main.java
Normal file
29
server/src/main/java/server/Main.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package server;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@EntityScan(basePackages = { "commons", "server" })
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class, args);
|
||||
}
|
||||
}
|
||||
17
server/src/main/java/server/SomeController.java
Normal file
17
server/src/main/java/server/SomeController.java
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package server;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/")
|
||||
public class SomeController {
|
||||
|
||||
@GetMapping("/")
|
||||
@ResponseBody
|
||||
public String index() {
|
||||
return "Hello world!";
|
||||
}
|
||||
}
|
||||
52
server/src/main/java/server/api/PersonListingController.java
Normal file
52
server/src/main/java/server/api/PersonListingController.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
* Copyright 2024 Sebastian Proksch
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package server.api;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import commons.Person;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/people")
|
||||
public class PersonListingController {
|
||||
|
||||
private List<Person> people = new LinkedList<>();
|
||||
|
||||
public PersonListingController() {
|
||||
people.add(new Person("Mickey", "Mouse"));
|
||||
people.add(new Person("Donald", "Duck"));
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Person> list() {
|
||||
return people;
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public List<Person> add(@RequestBody Person p) {
|
||||
if (!people.contains(p)) {
|
||||
people.add(p);
|
||||
}
|
||||
return people;
|
||||
}
|
||||
}
|
||||
79
server/src/main/java/server/api/QuoteController.java
Normal file
79
server/src/main/java/server/api/QuoteController.java
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package server.api;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import commons.Quote;
|
||||
import server.database.QuoteRepository;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/quotes")
|
||||
public class QuoteController {
|
||||
|
||||
private final Random random;
|
||||
private final QuoteRepository repo;
|
||||
|
||||
public QuoteController(Random random, QuoteRepository repo) {
|
||||
this.random = random;
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
@GetMapping(path = { "", "/" })
|
||||
public List<Quote> getAll() {
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Quote> getById(@PathVariable("id") long id) {
|
||||
if (id < 0 || !repo.existsById(id)) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
return ResponseEntity.ok(repo.findById(id).get());
|
||||
}
|
||||
|
||||
@PostMapping(path = { "", "/" })
|
||||
public ResponseEntity<Quote> add(@RequestBody Quote quote) {
|
||||
|
||||
if (quote.person == null || isNullOrEmpty(quote.person.firstName) || isNullOrEmpty(quote.person.lastName)
|
||||
|| isNullOrEmpty(quote.quote)) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
Quote saved = repo.save(quote);
|
||||
return ResponseEntity.ok(saved);
|
||||
}
|
||||
|
||||
private static boolean isNullOrEmpty(String s) {
|
||||
return s == null || s.isEmpty();
|
||||
}
|
||||
|
||||
@GetMapping("rnd")
|
||||
public ResponseEntity<Quote> getRandom() {
|
||||
var quotes = repo.findAll();
|
||||
var idx = random.nextInt((int) repo.count());
|
||||
return ResponseEntity.ok(quotes.get(idx));
|
||||
}
|
||||
}
|
||||
22
server/src/main/java/server/database/QuoteRepository.java
Normal file
22
server/src/main/java/server/database/QuoteRepository.java
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright 2021 Delft University of Technology
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package server.database;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import commons.Quote;
|
||||
|
||||
public interface QuoteRepository extends JpaRepository<Quote, Long> {}
|
||||
18
server/src/main/resources/application.properties
Normal file
18
server/src/main/resources/application.properties
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
|
||||
# use one of these alternatives...
|
||||
# ... purely in-memory, wiped on restart, but great for testing
|
||||
#spring.datasource.url=jdbc:h2:mem:testdb
|
||||
# ... persisted on disk (in project directory)
|
||||
spring.datasource.url=jdbc:h2:file:./h2-database
|
||||
|
||||
# enable DB view on http://localhost:8080/h2-console
|
||||
spring.h2.console.enabled=true
|
||||
|
||||
# strategy for table (re-)generation
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
# show auto-generated SQL commands
|
||||
#spring.jpa.hibernate.show_sql=true
|
||||
Loading…
Add table
Add a link
Reference in a new issue