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
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

import java.io.IOException;

@EnableEurekaServer
@SpringBootApplication
public class DiscoveryServer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ logging:
root: INFO
org.springframework: DEBUG
com.apress.cems: DEBUG

server:
port: 3000
eureka:
client:
registerWithEureka: false
fetchRegistry: false
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eureka.persons;

import com.eureka.persons.ex.NotFoundException;
import com.eureka.persons.person.Person;
import com.eureka.persons.services.PersonService;
import org.springframework.http.HttpStatus;
Expand All @@ -8,6 +9,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestController
Expand All @@ -26,7 +28,15 @@ public PersonsController(PersonService personService) {
@ResponseStatus(HttpStatus.OK)
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<Person> list() {
return new ArrayList<>();
List<Person> persons = personService.findAll();
persons.sort(new Comparator<Person>() {
@Override
public int compare(Person person1, Person person2) {
return person1.getId().compareTo(person2.getId());
}
});

return persons;
}

/**
Expand All @@ -36,6 +46,11 @@ public List<Person> list() {
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public void create(@RequestBody Person person, BindingResult result) {
if(result.hasErrors()) {
throw new PersonsException(HttpStatus.BAD_REQUEST, "Error when saving a person!.");
}

personService.save(person);
}

/**
Expand All @@ -48,7 +63,7 @@ public void create(@RequestBody Person person, BindingResult result) {
@ResponseStatus(HttpStatus.OK)
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public Person show(@PathVariable Long id) {
return new Person();
return personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id));
}

/**
Expand All @@ -62,6 +77,15 @@ public Person show(@PathVariable Long id) {
@ResponseStatus(HttpStatus.NO_CONTENT)
@PutMapping("/{id}")
public void update(@RequestBody Person updatedPerson, @PathVariable Long id) {
Person person = personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id));
person.setUsername(updatedPerson.getUsername());
person.setFirstName(updatedPerson.getFirstName());
person.setLastName(updatedPerson.getLastName());
person.setPassword(updatedPerson.getPassword());
person.setHiringDate(updatedPerson.getHiringDate());
person.setNewPassword(updatedPerson.getNewPassword());

personService.save(person);
}

/**
Expand All @@ -73,5 +97,6 @@ public void update(@RequestBody Person updatedPerson, @PathVariable Long id) {
@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
personService.delete(personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

import java.io.IOException;

@EntityScan(basePackages = "com.eureka.persons")
@SpringBootApplication
@EnableEurekaServer
public class PersonsServer {

private static Logger logger = LoggerFactory.getLogger(PersonsServer.class);
Expand Down
5 changes: 5 additions & 0 deletions eureka/persons-server/src/main/resources/persons-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ server:

# Discovery Server Access
#TODO here you add configurations for eureka client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka/
fetchRegistry: true

info:
app:
Expand Down
8 changes: 8 additions & 0 deletions lab-6-api-gateway/service1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
@EnableEurekaClient
@RequestMapping("api/greeting")
public class Service1Application {

public static void main(String[] args) {
Expand All @@ -16,5 +21,10 @@ public static void main(String[] args) {
// 3. print request headers
// 4. register the service in eureka

@GetMapping
public String getGreeting(String name) {
return "Hello " + name;
}


}
11 changes: 10 additions & 1 deletion lab-6-api-gateway/service1/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
spring:
application:
name: spring-cloud-eureka-client

server:
port: 8081
port: 8081

eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:3000/eureka}
49 changes: 49 additions & 0 deletions recipes-rest-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>programare-cloud-java</artifactId>
<groupId>com.unitbv</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>recipes-rest-service</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.recipes;

import org.springframework.boot.SpringApplication;

public class RecipesRestServiceApplication {
public static void main( String[] args )
{
SpringApplication.run(RecipesRestServiceApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.recipes.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

@Bean
public ObjectMapper objectMapper(){
return new ObjectMapper();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.recipes.controller;

import com.recipes.model.Ingredient;
import com.recipes.model.Recipe;
import com.recipes.service.RecipesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/recipe")
public class RecipesController {
private final RecipesService recipesService;

@Autowired
public RecipesController(RecipesService recipesService){
this.recipesService = recipesService;
}

@GetMapping()
public ResponseEntity<List<Recipe>> getAllRecipes(){
return ResponseEntity.ok(recipesService.getAllRecipes());
}

@PostMapping
public ResponseEntity<?> addRecipe(@RequestBody Recipe recipe) {
if(recipe.getName().trim().isEmpty() || recipe.getName() == null) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Recipe name cannot be empty or null!");
}

for(Ingredient ingredient : recipe.getIngredients()) {
if(ingredient.getName().trim().isEmpty() || ingredient.getName() == null) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Ingredient name cannot be empty or null!");
}
}

return ResponseEntity.ok(recipesService.addRecipe(recipe));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.recipes.datasource;

import com.recipes.model.Recipe;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Component
public class MyRecipes {
private List<Recipe> recipes;

public MyRecipes() { recipes = new ArrayList<>(); }

public List<Recipe> getAllRecipes() {
return recipes;
}

public Optional<Recipe> findRecipeByName(String name) {
return recipes.stream().filter(recipe -> recipe.getName().equals(name)).findFirst();
}

public Recipe addRecipe(Recipe recipe) {
this.recipes.add(recipe);

return recipe;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.recipes.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Ingredient {
private String name;
private double quantity;
private String unitOfMeasurement;
}
15 changes: 15 additions & 0 deletions recipes-rest-service/src/main/java/com/recipes/model/Recipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.recipes.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Recipe {
private String name;
private List<Ingredient> ingredients;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.recipes.service;

import com.recipes.model.Recipe;

import java.util.List;

public interface RecipesService {
List<Recipe> getAllRecipes();

Recipe addRecipe(Recipe recipe);
}
Loading