Skip to content

Commit

Permalink
Merge pull request #1 from whuera/develop
Browse files Browse the repository at this point in the history
Add new features for branch: develop
  • Loading branch information
whuera committed Jan 29, 2021
2 parents 9d80c3f + 10616f7 commit c8201f7
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 42 deletions.
26 changes: 26 additions & 0 deletions demoSpark/azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Maven
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'JaCoCo'
summaryFileLocation: '/site/cobertura/coverage.xml'
# Build your Java project and run tests with Apache Maven.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/java

trigger:
- main

pool:
vmImage: 'ubuntu-latest'

steps:
- task: Maven@3
inputs:
mavenPomFile: 'pom.xml'
mavenOptions: '-Xmx3072m'
javaHomeOption: 'JDKVersion'
jdkVersionOption: '1.8'
jdkArchitectureOption: 'x64'
publishJUnitResults: true
testResultsFiles: '**/surefire-reports/TEST-*.xml'
goals: 'package'
18 changes: 18 additions & 0 deletions demoSpark/src/main/java/com/app/demoSpark/models/Products.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.app.demoSpark.models;

import lombok.AllArgsConstructor;
import lombok.Data;

/** The type Products. */
@AllArgsConstructor
@Data
public class Products {
private double IdProduct;
private String codProduct;
private String nameProduct;
private String descProduct;
private String specificationsProduct;
private int quantityProduct;
private double costProduct;
private String urlImageProduct;
}
7 changes: 6 additions & 1 deletion demoSpark/src/main/java/com/app/demoSpark/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Optional;

/** The type User. */
@Data
@AllArgsConstructor
public class User {
private String id;
private String firstName;
private String lastName;
private String email;
private Optional<String> email;
private Optional<String> imageUrl;
private UserBasicData userBasicData;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.app.demoSpark.models;

import lombok.AllArgsConstructor;
import lombok.Data;

/** The type User basic data. */
@Data
@AllArgsConstructor
public class UserBasicData {
private int IdUserData;
private String typeIdentification;
private String numberIdentification;
private String genero;
private int age;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.app.demoSpark.service;

import com.app.demoSpark.models.Products;

import java.util.List;

public interface ProductService {
public void saveProduct(Products product);
public List<Products> getProducts();
public Products getProductById (String codeProduct);
public void editProduct(Products product);
public void deleteProduct(String codeProduct);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.app.demoSpark.service;

import com.app.demoSpark.models.Products;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** The type Product service. */
public class ProductServiceImpl implements ProductService {

private Map<String, Products> productsMap = new HashMap<String, Products>();

/** Instantiates a new Product service. */
public ProductServiceImpl() {
productsMap.put ("01", new Products (1,"PR-01", "DELL G3", "Laptop Dell G3 Gammer", "Intel Core I9, 16MB RAM, 1TG HDD, 15.6 Widescreen", 5, 1800, ""));
productsMap.put ("02", new Products (1,"PR-02", "DELL G7", "Laptop Dell G7 Gammer", "Intel Core I9, 16MB RAM, 1TG HDD, 17.3 Widescreen", 5, 1800, ""));
}

@Override
public void saveProduct (Products product) {
productsMap.put (String.valueOf(product.getIdProduct ()), product) ;
}

@Override
public List<Products> getProducts () {
return new ArrayList<Products> (productsMap.values ());
}

@Override
public Products getProductById (String codeProduct) {
return productsMap.get (codeProduct);
}

@Override
public void editProduct (Products product) {

}

@Override
public void deleteProduct (String codeProduct) {

}
}
24 changes: 24 additions & 0 deletions demoSpark/src/main/java/com/app/demoSpark/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,38 @@

import java.util.List;

/**
* Interface for UserService
*/
public interface UserService {
/**
*
* @param user
*/
public void addUSer(User user);

/**
*
* @param user
*/
public void editUser(User user);

/**
*
* @param id
* @return
*/
public User getUserById(String id);

/**
*
* @param id
*/
public void deleteUserById(String id);

/**
*
* @return
*/
public List<User> getUsers();
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package com.app.demoSpark.service;

import com.app.demoSpark.models.User;
import com.app.demoSpark.models.UserBasicData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class UserServiceImpl implements UserService{
/** The type User service. */
public class UserServiceImpl implements UserService {
private static Map<String, User> users = new HashMap<String, User> ();

public UserServiceImpl(){
users.put("101", new User("101","Pavan Solapure","Perez", "pavan.solapure@gmail.com"));
users.put("102", new User("102","Aadya Solapure","Perez","aadya.solapure@gmail.com"));
users.put("103", new User("103","Aaarna Solapure","Perez", "aarna.solapure@gmail.com"));
users.put("104", new User("104","Shilpa Solapure","Perez","shilpa.solapure@gmail.com"));
/** Instantiates a new User service. */
public UserServiceImpl() {

users.put("101", new User("101","Pavan Solapure","Perez", Optional.ofNullable("pavan.solapure@gmail.com"), Optional.ofNullable ("https://bucket-mobilpymes.s3.amazonaws.com/img/a1.jpg"), new UserBasicData(1, "1","15126622","M",30)));
users.put("102", new User("102","Aadya Solapure","Perez",Optional.ofNullable("aadya.solapure@gmail.com"), Optional.ofNullable ("https://bucket-mobilpymes.s3.amazonaws.com/img/a2.jpg"), new UserBasicData(2, "1","15126622","M",30)));
users.put("103", new User("103","Aaarna Solapure","Perez", Optional.ofNullable("aarna.solapure@gmail.com"), Optional.ofNullable ("https://bucket-mobilpymes.s3.amazonaws.com/img/a3.jpg"), new UserBasicData(3, "1","15126622","M",30)));
users.put("104", new User("104","Shilpa Solapure","Perez",Optional.ofNullable("shilpa.solapure@gmail.com"), Optional.ofNullable ("https://bucket-mobilpymes.s3.amazonaws.com/img/a4.jpg"), new UserBasicData(4, "1","15126622","M",30)));
users.put("105", new User("105","Pavan Solapure","Perez", Optional.ofNullable("pavan.solapure@gmail.com"), Optional.ofNullable ("https://bucket-mobilpymes.s3.amazonaws.com/img/a5.jpg"), new UserBasicData(1, "1","15126622","M",30)));
users.put("106", new User("106","Aadya Solapure","Perez",Optional.ofNullable("aadya.solapure@gmail.com"), Optional.ofNullable ("https://bucket-mobilpymes.s3.amazonaws.com/img/a6.jpg"), new UserBasicData(2, "1","15126622","M",30)));
users.put("107", new User("107","Aaarna Solapure","Perez", Optional.ofNullable("aarna.solapure@gmail.com"), Optional.ofNullable ("https://bucket-mobilpymes.s3.amazonaws.com/img/a7.jpg"), new UserBasicData(3, "1","15126622","M",30)));
users.put("108", new User("108","Shilpa Solapure","Perez",Optional.ofNullable("shilpa.solapure@gmail.com"), Optional.ofNullable ("https://bucket-mobilpymes.s3.amazonaws.com/img/a8.jpg"), new UserBasicData(4, "1","15126622","M",30)));

}

@Override
Expand Down
104 changes: 73 additions & 31 deletions demoSpark/src/main/java/com/app/demoSpark/sparkMain.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,105 @@
package com.app.demoSpark;

import com.app.demoSpark.models.Products;
import com.app.demoSpark.models.User;
import com.app.demoSpark.service.ProductService;
import com.app.demoSpark.service.ProductServiceImpl;
import com.app.demoSpark.service.UserService;
import com.app.demoSpark.service.UserServiceImpl;
import com.google.gson.Gson;
import static spark.Spark.delete;
import static spark.Spark.get;
import static spark.Spark.port;
import static spark.Spark.put;
import static spark.Spark.post;

import java.util.List;

import static spark.Spark.*;


public class sparkMain {
public static void main(String[] args) {
public static void main (String[] args) {

UserService userService = new UserServiceImpl ();
UserService userService = new UserServiceImpl ();
ProductService productService = new ProductServiceImpl ();

//configure port
port(8080);
port (8080);

get("/hello", (req, res)->"Hello, world!");
get ("/hello", (req, res) -> "Hello, world!");

get("/hello/:name", (req,res)->{
return "Hello, "+ req.params(":name") + "!!";
get ("/hello/:name", (req, res) -> {
return "Hello, " + req.params (":name") + "!!";
});

post("/users/add", (req,res)-> {
res.type("application/json");

User user = new Gson().fromJson(req.body(), User.class);
options ("/*",
(request, response) -> {
String accessControlRequestHeaders = request
.headers("Access-Control-Request-Headers");
if (accessControlRequestHeaders != null) {
response.header("Access-Control-Allow-Headers",
accessControlRequestHeaders);
}
String accessControlRequestMethod = request
.headers("Access-Control-Request-Method");
if (accessControlRequestMethod != null) {
response.header("Access-Control-Allow-Methods",
accessControlRequestMethod);
}
return "OK";
});
before((request, response) -> response.header("Access-Control-Allow-Origin", "*"));

post ("/users/add", (req, res) -> {
res.type ("application/json");

User user = new Gson ().fromJson (req.body (), User.class);
userService.addUSer (user);
return new Gson().toJson(user);
return new Gson ().toJson (user);
});

get("/users", (req,res)-> {
res.type("application/json");
return new Gson().toJson(userService.getUsers());
get ("/users", (req, res) -> {
res.type ("application/json");
return new Gson ().toJson (userService.getUsers ());
});

delete("/users/delete/:id", (req,res) -> {
res.type("application/json");
userService.deleteUserById(req.params(":id"));
return new Gson().toJson("User deleted");
get ("/users/:id", (req, res) -> {
res.type ("application/json");
return new Gson ().toJson (userService.getUserById (req.params (":id")));
});

put("/users/edit/:id", (req,res) -> {
res.type("application/json");
User ue = new Gson().fromJson(req.body(), User.class);
delete ("/users/delete/:id", (req, res) -> {
res.type ("application/json");
userService.deleteUserById (req.params (":id"));
return new Gson ().toJson ("User deleted");
});

User searchedUser = userService.getUserById(ue.getId());
if(searchedUser != null) {
userService.editUser(ue);
return new Gson().toJson(ue);
put ("/users/edit/:id", (req, res) -> {
res.type ("application/json");
User ue = new Gson ().fromJson (req.body (), User.class);

User searchedUser = userService.getUserById (ue.getId ());
if (searchedUser != null) {
userService.editUser (ue);
return new Gson ().toJson (ue);
} else {
return new Gson().toJson("User not found or error in edit");
return new Gson ().toJson ("User not found or error in edit");
}

});

get ("/products", (req, res) -> {
res.type ("application/json");
List<Products> productsList = productService.getProducts ();
if (productsList.size () > 0){
return new Gson ().toJson (productsList);
} else {
return new Gson ().toJson ("No existe productos en stock");
}
});
/**
* method from request by idProducts
*/
get ("/products/:id", (req, res) -> {
res.type ("application/json");
return new Gson ().toJson (productService.getProductById (req.params (":id")));
});

}
}

0 comments on commit c8201f7

Please sign in to comment.