Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/technical test rodrigue convalot #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions technical-test-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
package technical.test.api.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver;
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@EnableMongoRepositories
@Configuration
@EnableMongoRepositories(basePackages = "technical.test.api.repository")
public class MongoDBConfig {

@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private MongoConverter mongoConverter;

@EventListener(ApplicationReadyEvent.class)
public void initIndicesAfterStartup() {
var mappingContext = this.mongoConverter.getMappingContext();
if (mappingContext instanceof MongoMappingContext) {
var mongoMappingContext = (MongoMappingContext) mappingContext;
for (BasicMongoPersistentEntity<?> persistentEntity : mongoMappingContext.getPersistentEntities()) {
var clazz = persistentEntity.getType();
if (clazz.isAnnotationPresent(Document.class)) {
var indexOps = mongoTemplate.indexOps(clazz);
var resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
resolver.resolveIndexFor(clazz).forEach(indexOps::ensureIndex);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package technical.test.api.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import technical.test.api.exception.ProductAlreadyExistsException;
import technical.test.api.model.Response;
import technical.test.api.model.dto.ProductDTO;
import technical.test.api.services.IProductService;

@RestController
@RequestMapping("/api/products")
public class ProductController {

@Autowired
private IProductService productService;

// se déclenche lors de l'ajout d'un produit déjà existant
@ExceptionHandler(ProductAlreadyExistsException.class)
@ResponseStatus(HttpStatus.CONFLICT)
public String onProductAlreadyExistsError(Exception e) {
return e.getMessage();
}

// renvoie la liste de tous les produits
@GetMapping("/")
@CrossOrigin(origins = "http://localhost:8081")
public Response<Collection<ProductDTO>> findAllProducts() {
Collection<ProductDTO> products = this.productService.findAll();

return new Response<>(products, HttpStatus.OK);
}

// insert 3 produits par defaut
@PutMapping("/insert")
@ResponseStatus(HttpStatus.CREATED)
public void insertSomeProducts() {
this.productService.insertSomeProducts();
}

// ajoute un nouveau produit
@PostMapping("/")
public Response<ProductDTO> createProduct(@RequestBody ProductDTO productDTO) {
ProductDTO newProductDto = this.productService.create(productDTO);

return new Response<>(newProductDto, HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package technical.test.api.exception;

public class ProductAlreadyExistsException extends RuntimeException {

// renvoie un message d'erreur lorsqu'un produit existe déjà
public ProductAlreadyExistsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package technical.test.api.mapper;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.stereotype.Service;

import technical.test.api.model.Product;
import technical.test.api.model.dto.ProductDTO;

@Service
public class ProductMapper {

public Product toEntity(ProductDTO productDto) {
Product product = new Product();

product.setReference(productDto.getReference());
product.setName(productDto.getName());
product.setPrice(productDto.getPrice());

return product;
}

public ProductDTO toDto(Product product) {
ProductDTO productDto = new ProductDTO();

productDto.setReference(product.getReference());
productDto.setName(product.getName());
productDto.setPrice(product.getPrice());

return productDto;
}

public Collection<Product> toEntity(Collection<ProductDTO> productDtos) {
Collection<Product> products = new ArrayList<Product>();

for (ProductDTO productDto : productDtos) {
products.add(this.toEntity(productDto));
}

return products;
}

public Collection<ProductDTO> toDto(Collection<Product> products) {
Collection<ProductDTO> productDtos = new ArrayList<ProductDTO>();

for (Product product : products) {
productDtos.add(this.toDto(product));
}

return productDtos;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package technical.test.api.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

@Document(collection = "Product")
public class Product {

@Id
private String id;
@Indexed(unique = true)
@Field(value = "reference")
private String reference;
@Field(value = "name")
private String name;
@Field(value = "price")
private double price;

public Product() {
super();
}

public Product(String reference, String name, double price) {
super();
this.reference = reference;
this.name = name;
this.price = price;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getReference() {
return reference;
}

public void setReference(String reference) {
this.reference = reference;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

@Override
public String toString() {
return "Product [id=" + id + ", reference=" + reference + ", name=" + name + ", price=" + price + "]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
Loading