Skip to content

Commit

Permalink
Merge pull request #3 from SwevenSoftware/feature/refactor
Browse files Browse the repository at this point in the history
Feature/refactor
  • Loading branch information
nR3D authored Feb 14, 2021
2 parents 34020d7 + 3f29de0 commit 7eb4c11
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build
log
log*
db
.gradle
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# BlockCOVID-server
Modulo Server per il progetto di SWE 2020/2021 - BlockCOVID

## Come usare mongo
Sono inclusi degli script per utilizzare mongo per testare come si comporta:
- **Avvio**: `./spawnMongo.sh`, spawna un'istanza di `mongod` che lavora sulla cartella locale `./db` e raccoglie i log nel file `log`. Fallisce se ci sono altre istanze già attive.
- **Stop**: `./killMongo.sh`, termina l'istanza di `mongod` avviata con `spawnMongo.sh`. Attenzione che fallisce con tutte le altre istanze di mongodb.
- **Una volta avviato**: è possibile utilizzare la shell tramite il comando `mongo`.
26 changes: 17 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

group = 'it.sweven.blockcovid'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
// Spring dependencies
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
implementation 'org.springframework.boot:spring-boot-starter-web'
// MongoDB Drivers
implementation 'org.mongodb:mongodb-driver-reactivestreams'
// reactivex reactive streams
implementation 'io.reactivex.rxjava2:rxjava'
implementation 'io.reactivex:rxjava-reactive-streams'

// TESTS
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}

test {
useJUnitPlatform()
useJUnitPlatform()
}
3 changes: 3 additions & 0 deletions killMongo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

mongod --shutdown -dbpath db/
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'demo'
rootProject.name = 'blockcovid'
4 changes: 3 additions & 1 deletion spawnMongo.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/sh

mongod --fork --dbpath ./db/ --logpath ./log
echo `pwd`
[ ! -d db/ ] && mkdir db/
mongod --fork --dbpath db/ --logpath log
13 changes: 0 additions & 13 deletions src/main/java/it/sweven/blockcovid/Data.java

This file was deleted.

26 changes: 0 additions & 26 deletions src/main/java/it/sweven/blockcovid/Entita.java

This file was deleted.

9 changes: 0 additions & 9 deletions src/main/java/it/sweven/blockcovid/EntitaRepo.java

This file was deleted.

21 changes: 0 additions & 21 deletions src/main/java/it/sweven/blockcovid/LoadMongoDB.java

This file was deleted.

31 changes: 0 additions & 31 deletions src/main/java/it/sweven/blockcovid/MongoController.java

This file was deleted.

62 changes: 62 additions & 0 deletions src/main/java/it/sweven/blockcovid/Router.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package it.sweven.blockcovid;

/* Spring Annotations */
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/* Bean factory annotations */
import org.springframework.beans.factory.annotation.Autowired;

/* Reactor core imports */
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/* Our own imports */
import it.sweven.blockcovid.entities.User;
import it.sweven.blockcovid.repositories.UserRepository;

@RestController
class Router {

@Autowired
private final UserRepository repository;

Router(UserRepository repo) {
this.repository = repo;
}

@GetMapping("/")
String hello() {
return "Pagina iniziale!!\n";
}

@GetMapping("/init")
String init() {
repository.save(new User("Gianni", "password"));
repository.save(new User("Annaclara", "password"));
repository.save(new User("Mario", "password"));
repository.save(new User("Genni", "password"));

return "Init avvenuto";
}

@GetMapping("/user/{name}")
Mono<User> name(@PathVariable String name) {
return repository.findById(name);
}

@PostMapping("/user/new")
@ResponseBody
Mono<User> insert(@RequestBody User newUser) {
return repository.save(newUser);
}

@GetMapping("/user/all")
Flux<User> all(){
return repository.findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MongoDemo {
public class Server {

public static void main(String[] args) {
SpringApplication.run(MongoDemo.class, args);
SpringApplication.run(Server.class, args);
}

}
23 changes: 23 additions & 0 deletions src/main/java/it/sweven/blockcovid/ServerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package it.sweven.blockcovid;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;

import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;

@Configuration
@EnableReactiveMongoRepositories(basePackages = "it.sweven.blockcovid.repositories")
public class ServerConfig extends AbstractReactiveMongoConfiguration {

@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
}

@Override
protected String getDatabaseName() {
return "blockcovid-reactive";
}
}
34 changes: 34 additions & 0 deletions src/main/java/it/sweven/blockcovid/entities/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package it.sweven.blockcovid.entities;

/* Java utilities */
import java.util.Set;

/* Spring utilities */
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class User {

private @Id String name;
private String password;
// private Token token;
// private Set<Authorization> authorizations;

public User(String name, String password) {
this.name = name;
this.password = password;
}

// public boolean isUser() { return false; }

// public boolean isAdmin() { return false; }

public String getName() { return name; }

@Override
public String toString() {
return String.format("{'name': '%s', 'password': '%s'}",
name, password);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package it.sweven.blockcovid.repositories;

/* Spring imports */
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;

/* Our imports */
import it.sweven.blockcovid.entities.User;

public interface UserRepository extends ReactiveMongoRepository<User, String> {
/* l'implementazione può rimanere vuota, La classe di base si
* occupa di fare tutto */
}

0 comments on commit 7eb4c11

Please sign in to comment.