From 826598962f1fe144d880fd436d17aaaafc2acfa8 Mon Sep 17 00:00:00 2001 From: agyen Date: Mon, 24 Feb 2020 16:07:31 +0000 Subject: [PATCH 01/18] request service and endpoints --- .../Controllers/RequestController.java | 62 ++++++++++++ .../Models/RequestTO.java | 96 +++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java create mode 100644 src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java new file mode 100644 index 0000000..3d7c038 --- /dev/null +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -0,0 +1,62 @@ +package io.turntabl.employementprofilingsystem.Controllers; + +import io.swagger.annotations.ApiOperation; +import io.turntabl.employementprofilingsystem.Models.RequestTO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.BeanPropertyRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.*; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.text.SimpleDateFormat; +import java.util.List; + +@RestController +public class RequestController { + + @Autowired + JdbcTemplate jdbcTemplate; + + @CrossOrigin + @ApiOperation("Make a request") + @PostMapping("/api/v1/request") + public void makeARequest(@RequestBody RequestTO request) { + jdbcTemplate.update("insert into requests(requester_id, request_start_date, request_report_date) values(?,?,?)", + request.getRequester_id(), request.getRequest_start_date(), request.getRequest_report_date()); + } + + @CrossOrigin + @ApiOperation("Get all requests for requester") + @GetMapping("/api/v1/request/requester/{id}") + public List getRequestByRequesterId(@PathVariable("id") Integer id) { + return this.jdbcTemplate.query( + "select * from requests where requester_id = ?", + new Object[]{id}, + new BeanPropertyRowMapper<>(RequestTO.class) + ); + } + + @CrossOrigin + @ApiOperation("Get all requests") + @GetMapping("/api/v1/requests") + public List getAllRequests() { + return this.jdbcTemplate.query("select * from requests", + new BeanPropertyRowMapper(RequestTO.class) + ); + } + + @CrossOrigin + @ApiOperation("Accept Request") + @PutMapping("/api/v1/requests/approve/{id}") + public void approveRequest(@PathVariable("id") Integer request_id) { + this.jdbcTemplate.update("update requests set request_status_id = 3 where request_status_id = 1 and request_id = ?", request_id); + } + + @CrossOrigin + @ApiOperation("Decline Request") + @PutMapping("/api/v1/requests/decline/{id}") + public void declineRequest(@PathVariable("id") Integer request_id) { + this.jdbcTemplate.update("update requests set request_status_id = 2 where request_status_id = 1 and request_id = ?", request_id); + } +} + diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java new file mode 100644 index 0000000..11bb712 --- /dev/null +++ b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java @@ -0,0 +1,96 @@ +package io.turntabl.employementprofilingsystem.Models; + +import java.util.Date; + +public class RequestTO { + private int request_id; + private int requester_id; + private Date request_start_date; + private Date request_report_date; + private int request_status_id; + private String from; + private String requester_name; + + public RequestTO() { + } + + public RequestTO(int request_id, int requester_id, Date request_start_date, Date request_report_date, int request_status_id, String from, String requester_name) { + this.request_id = request_id; + this.requester_id = requester_id; + this.request_start_date = request_start_date; + this.request_report_date = request_report_date; + this.request_status_id = request_status_id; + this.from = from; + this.requester_name = requester_name; + } + + public int getRequest_id() { + return request_id; + } + + public void setRequest_id(int request_id) { + this.request_id = request_id; + } + + public int getRequester_id() { + return requester_id; + } + + public void setRequester_id(int requester_id) { + this.requester_id = requester_id; + } + + public Date getRequest_start_date() { + return request_start_date; + } + + public void setRequest_start_date(Date request_start_date) { + this.request_start_date = request_start_date; + } + + public Date getRequest_report_date() { + return request_report_date; + } + + public void setRequest_report_date(Date request_report_date) { + this.request_report_date = request_report_date; + } + + public int getRequest_status_id() { + return request_status_id; + } + + public void setRequest_status_id(int request_status_id) { + this.request_status_id = request_status_id; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getRequester_name() { + return requester_name; + } + + public void setRequester_name(String requester_name) { + this.requester_name = requester_name; + } + + @Override + public String toString() { + return "RequestTO{" + + "request_id=" + request_id + + ", requester_id=" + requester_id + + ", request_start_date=" + request_start_date + + ", request_report_date=" + request_report_date + + ", request_status_id=" + request_status_id + + ", from='" + from + '\'' + + ", requester_name='" + requester_name + '\'' + + '}'; + } +} + From c465e75491c68dada11955b8e0df4c2cc47df176 Mon Sep 17 00:00:00 2001 From: agyen Date: Mon, 24 Feb 2020 17:37:53 +0000 Subject: [PATCH 02/18] getting specific colomn from requests table --- .../Controllers/RequestController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java index 3d7c038..4640633 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -30,7 +30,8 @@ public void makeARequest(@RequestBody RequestTO request) { @GetMapping("/api/v1/request/requester/{id}") public List getRequestByRequesterId(@PathVariable("id") Integer id) { return this.jdbcTemplate.query( - "select * from requests where requester_id = ?", + "select select request_start_date, request_report_date, request_status.req_status from requests inner join request_status on " + + "requests.request_status_id = request_status.request_status_id where requester_id = ?", new Object[]{id}, new BeanPropertyRowMapper<>(RequestTO.class) ); From c1405569a0af8d61fd8927462efbf57fb2f351cb Mon Sep 17 00:00:00 2001 From: agyen Date: Tue, 25 Feb 2020 08:14:45 +0000 Subject: [PATCH 03/18] getting specific columns --- .../Controllers/RequestController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java index 4640633..289756c 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -41,7 +41,8 @@ public List getRequestByRequesterId(@PathVariable("id") Integer id) { @ApiOperation("Get all requests") @GetMapping("/api/v1/requests") public List getAllRequests() { - return this.jdbcTemplate.query("select * from requests", + return this.jdbcTemplate.query("select select request_start_date, request_report_date, request_status.req_status from requests inner join request_status on" + + "requests.request_status_id = request_status.request_status_id", new BeanPropertyRowMapper(RequestTO.class) ); } From 5cabb138892160645e6d52e695f6ed39c37f67c2 Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 10:27:55 +0000 Subject: [PATCH 04/18] employee db extended with docker files --- Dockerfile | 5 + docker-compose.yml | 33 ++++ init.sh | 45 ++++++ .../Controllers/EmployeeController.java | 14 +- .../Controllers/RequestController.java | 61 +++++++ .../DAO/EmployeeDAO.java | 2 +- .../Gmail/Email.java | 152 ++++++++++++++++++ .../Gmail/GmailService.java | 98 +++++++++++ .../Models/RequestTO.java | 95 +++++++++++ 9 files changed, 497 insertions(+), 8 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 init.sh create mode 100644 src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java create mode 100644 src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java create mode 100644 src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java create mode 100644 src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..85cbe21 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM adoptopenjdk:11-jre-hotspot +COPY build/libs/employement-profiling-system-0.0.1-SNAPSHOT.jar app.jar +RUN chmod 777 app.jar + +ENTRYPOINT [ "java", "-jar", "app.jar" ] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..20da902 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,33 @@ +version: '3' +services: + apigateway: + image: "idawud/turntablapigateway:latest" + env_file: + - .envs/.apigateway + ports: + - "80:8081" + permission: + image: "idawud/accperm:v3" + env_file: + - .envs/.permission + ports: + - "5000:5000" + gis: + image: "isammyk/gis-docker:latest" + env_file: + - .envs/.gis + ports: + - "5004:5004" + chess: + image: "francisbilla/chess:latest" + env_file: + - .envs/.chess + ports: + - "8080:8080" + holidayrequest: + image: " billalidocker/employee-service:latest" + env_file: + - .envs/.holidayrequest + ports: + - "7070:7070" + diff --git a/init.sh b/init.sh new file mode 100644 index 0000000..7fcb27f --- /dev/null +++ b/init.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +#create init env files: list all services in the array, separeted by space +declare -a arr=("apigateway" "gis" "permission" "chess" "holidayrequest") + +for i in "${arr[@]}" +do + filename="./.envs/.$i" + if [[ ! -e "$filename" ]]; then + mkdir -p ./.envs + touch $filename + fi +done + +# install docker +if docker --version 2>&1 >/dev/null ; then + echo >&2 "docker installed" +else + sudo apt install docker -y +fi + +#install docker-compose +if docker-compose --version 2>&1 >/dev/null ; then + echo >&2 "docker-compose installed" +else + sudo apt install docker-compose -y +fi + +# run docker-compose +declare -i count=$(docker-compose ps | wc -l) +if [ $count -gt 2 ]; then + echo "Restarting services:...." + sudo docker-compose down + sudo docker-compose pull + sudo docker rmi $(docker images -a | grep '' | awk '{print $3}') + sudo docker-compose build --no-cache + sudo docker-compose up -d +else + echo "Starting all services:....." + sudo docker-compose down + sudo docker-compose pull + sudo docker rmi $(docker images -a | grep '' | awk '{print $3}') + sudo docker-compose build --no-cache + sudo docker-compose up -d +fi \ No newline at end of file diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/EmployeeController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/EmployeeController.java index fbca8f2..0cad43c 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/EmployeeController.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/EmployeeController.java @@ -2,11 +2,11 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; - import io.turntabl.employementprofilingsystem.DAO.EmployeeDAO; import io.turntabl.employementprofilingsystem.Models.AddEmployee; import io.turntabl.employementprofilingsystem.Models.EditEmployee; -import io.turntabl.employementprofilingsystem.Transfers.*; +import io.turntabl.employementprofilingsystem.Transfers.Employee; +import io.turntabl.employementprofilingsystem.Transfers.UpdateEmployee; import io.turntabl.employementprofilingsystem.Utilities.Date; import io.turntabl.employementprofilingsystem.Utilities.Parsor; import org.springframework.beans.factory.annotation.Autowired; @@ -14,14 +14,14 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.web.bind.annotation.*; - -import java.sql.SQLException; -import java.util.*; -import java.util.stream.Collectors; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; @Api @RestController -class EmployeeController implements EmployeeDAO{ +class EmployeeController implements EmployeeDAO { @Autowired JdbcTemplate jdbcTemplate; diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java new file mode 100644 index 0000000..4945b80 --- /dev/null +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -0,0 +1,61 @@ +package io.turntabl.employementprofilingsystem.Controllers; + +import io.swagger.annotations.ApiOperation; +import io.turntabl.employementprofilingsystem.Gmail.Email; +import io.turntabl.employementprofilingsystem.Models.RequestTO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.BeanPropertyRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.*; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.text.SimpleDateFormat; +import java.util.List; + +@RestController +public class RequestController { + + @Autowired + JdbcTemplate jdbcTemplate; + + @CrossOrigin + @ApiOperation("Make a request") + @PostMapping("/api/v1/request") + public void makeARequest(@RequestBody RequestTO request) { + jdbcTemplate.update("insert into requests(requester_id, request_start_date, request_report_date) values(?,?,?)", + request.getRequester_id(), request.getRequest_start_date(), request.getRequest_report_date()); + + SimpleDateFormat DateFor = new SimpleDateFormat("E, dd MMMM yyyy"); + String startDate = DateFor.format(request.getRequest_start_date()); + String reportDate = DateFor.format(request.getRequest_report_date()); + + try { + Email.requestMessage("isaac.agyen@turntabl.io", request.getFrom() ,"Holiday request", startDate, reportDate, request.getRequester_name()); + } catch (IOException e) { + e.printStackTrace(); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + } + } + + @CrossOrigin + @ApiOperation("Get all requests for requester") + @GetMapping("/api/v1/request/requester/{id}") + public List getRequestByRequesterId(@PathVariable("id") Integer id){ + return this.jdbcTemplate.query( + "select * from requests where requester_id = ?", + new Object[]{id}, + new BeanPropertyRowMapper<>(RequestTO.class) + ); + } + + @CrossOrigin + @ApiOperation("Get all requests") + @GetMapping("/api/v1/requests") + public List getAllRequests(){ + return this.jdbcTemplate.query("select * from requests", + new BeanPropertyRowMapper(RequestTO.class) + ); + } + +} diff --git a/src/main/java/io/turntabl/employementprofilingsystem/DAO/EmployeeDAO.java b/src/main/java/io/turntabl/employementprofilingsystem/DAO/EmployeeDAO.java index 5ede3ce..8d019e6 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/DAO/EmployeeDAO.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/DAO/EmployeeDAO.java @@ -13,7 +13,7 @@ public interface EmployeeDAO { @PostMapping("/v1/api/employee") Map addEmployee(@RequestBody AddEmployee requestData); - @ApiOperation("List of Employee Profile") + @ApiOperation("List of Employee") @CrossOrigin(origins = "*") @GetMapping("/v1/api/employees") Map getAllEmployee(); diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java new file mode 100644 index 0000000..c5aaed9 --- /dev/null +++ b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java @@ -0,0 +1,152 @@ +package io.turntabl.employementprofilingsystem.Gmail; + +import java.io.IOException; +import java.security.GeneralSecurityException; + +public class Email { + public static void requestMessage(String to, String from, String subject, String request_start_date, String request_report_date, String requester_name) throws IOException, GeneralSecurityException { + String bodyText = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " Pollstar\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
 \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + "

Hi there,

\n" + + "

Holiday request by " + requester_name + "

\n" + + "

Start date: " + request_start_date + "

\n" + + "

Report date: " + request_report_date +"

\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
Take Action
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " Turntabl, Achimota, Mile 7, Sonnidom house\n" + + "
Turntabl.\n" + + "
\n" + + "
\n" + + "
\n" + + "
 
\n" + + " \n" + + " "; + + GmailService.sendMail(from, to, subject, bodyText.toString()); + } +} \ No newline at end of file diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java new file mode 100644 index 0000000..defedb1 --- /dev/null +++ b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java @@ -0,0 +1,98 @@ +package io.turntabl.employementprofilingsystem.Gmail; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.Base64; +import com.google.api.services.gmail.Gmail; +import com.google.api.services.gmail.GmailScopes; +import com.google.api.services.gmail.model.Message; +import com.google.common.collect.ImmutableList; + +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.List; +import java.util.Properties; + +public class GmailService { + private static final String APPLICATION_NAME = "GmailService"; + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + + private static final List SCOPES = ImmutableList.of(GmailScopes.GMAIL_SEND, GmailScopes.GMAIL_COMPOSE); + private static final String CREDENTIALS_FILE_PATH = "credentials.json"; + + public static void sendMail(String from, String to, String subject, String bodyText) throws IOException, GeneralSecurityException { + Gmail service = getGmail(); + System.out.println(service.getApplicationName()); + System.out.println(service.getBaseUrl()); + System.out.println(service.users().toString()); + + String user = from; + try { + service.users().messages().send(user, + createMessageWithEmail(createEmail(to, from, subject, bodyText)) + ).execute(); + } catch (MessagingException e) { + e.printStackTrace(); + } + } + + public static Gmail getGmail() throws GeneralSecurityException, IOException { + HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + + GoogleCredential gcFromJson = GoogleCredential + .fromStream(new FileInputStream(CREDENTIALS_FILE_PATH)) + .createScoped(SCOPES); + + GoogleCredential credential = new GoogleCredential.Builder() + .setTransport(gcFromJson.getTransport()) + .setJsonFactory(gcFromJson.getJsonFactory()) + .setServiceAccountId(gcFromJson.getServiceAccountId()) + .setServiceAccountUser( "dawud.ismail@turntabl.io") + .setServiceAccountPrivateKey(gcFromJson.getServiceAccountPrivateKey()) + .setServiceAccountScopes(gcFromJson.getServiceAccountScopes()) + .setTokenServerEncodedUrl(gcFromJson.getTokenServerEncodedUrl()) + .build(); + boolean token = credential.refreshToken(); + System.out.println(token); + return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) + .setApplicationName(APPLICATION_NAME) + .build(); + } + + private static Message createMessageWithEmail(MimeMessage emailContent) + throws MessagingException, IOException { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + emailContent.writeTo(buffer); + byte[] bytes = buffer.toByteArray(); + String encodedEmail = Base64.encodeBase64URLSafeString(bytes); + Message message = new Message(); + message.setRaw(encodedEmail); + return message; + } + + private static MimeMessage createEmail(String to, + String from, + String subject, + String bodyText) throws MessagingException { + Properties props = new Properties(); + Session session = Session.getDefaultInstance(props, null); + + MimeMessage email = new MimeMessage(session); + + email.setFrom(new InternetAddress( from )); + email.addRecipient(javax.mail.Message.RecipientType.TO, + new InternetAddress(to)); + email.setSubject(subject); + email.setText(bodyText, "UTF-8", "html"); + return email; + } +} diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java new file mode 100644 index 0000000..91dfa9b --- /dev/null +++ b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java @@ -0,0 +1,95 @@ +package io.turntabl.employementprofilingsystem.Models; + +import java.util.Date; + +public class RequestTO { + private int request_id; + private int requester_id; + private Date request_start_date; + private Date request_report_date; + private int request_status_id; + private String from; + private String requester_name; + + public RequestTO() { + } + + public RequestTO(int request_id, int requester_id, Date request_start_date, Date request_report_date, int request_status_id, String from, String requester_name) { + this.request_id = request_id; + this.requester_id = requester_id; + this.request_start_date = request_start_date; + this.request_report_date = request_report_date; + this.request_status_id = request_status_id; + this.from = from; + this.requester_name = requester_name; + } + + public int getRequest_id() { + return request_id; + } + + public void setRequest_id(int request_id) { + this.request_id = request_id; + } + + public int getRequester_id() { + return requester_id; + } + + public void setRequester_id(int requester_id) { + this.requester_id = requester_id; + } + + public Date getRequest_start_date() { + return request_start_date; + } + + public void setRequest_start_date(Date request_start_date) { + this.request_start_date = request_start_date; + } + + public Date getRequest_report_date() { + return request_report_date; + } + + public void setRequest_report_date(Date request_report_date) { + this.request_report_date = request_report_date; + } + + public int getRequest_status_id() { + return request_status_id; + } + + public void setRequest_status_id(int request_status_id) { + this.request_status_id = request_status_id; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getRequester_name() { + return requester_name; + } + + public void setRequester_name(String requester_name) { + this.requester_name = requester_name; + } + + @Override + public String toString() { + return "RequestTO{" + + "request_id=" + request_id + + ", requester_id=" + requester_id + + ", request_start_date=" + request_start_date + + ", request_report_date=" + request_report_date + + ", request_status_id=" + request_status_id + + ", from='" + from + '\'' + + ", requester_name='" + requester_name + '\'' + + '}'; + } +} From 515aae11766fcab4939e5301b895fe8b5043cb23 Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 10:42:08 +0000 Subject: [PATCH 05/18] before adding request files --- Dockerfile | 5 - docker-compose.yml | 33 ---- init.sh | 45 ------ .../Controllers/RequestController.java | 61 ------- .../Gmail/Email.java | 152 ------------------ .../Gmail/GmailService.java | 98 ----------- .../Models/RequestTO.java | 95 ----------- 7 files changed, 489 deletions(-) diff --git a/Dockerfile b/Dockerfile index 85cbe21..e69de29 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +0,0 @@ -FROM adoptopenjdk:11-jre-hotspot -COPY build/libs/employement-profiling-system-0.0.1-SNAPSHOT.jar app.jar -RUN chmod 777 app.jar - -ENTRYPOINT [ "java", "-jar", "app.jar" ] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 20da902..e69de29 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,33 +0,0 @@ -version: '3' -services: - apigateway: - image: "idawud/turntablapigateway:latest" - env_file: - - .envs/.apigateway - ports: - - "80:8081" - permission: - image: "idawud/accperm:v3" - env_file: - - .envs/.permission - ports: - - "5000:5000" - gis: - image: "isammyk/gis-docker:latest" - env_file: - - .envs/.gis - ports: - - "5004:5004" - chess: - image: "francisbilla/chess:latest" - env_file: - - .envs/.chess - ports: - - "8080:8080" - holidayrequest: - image: " billalidocker/employee-service:latest" - env_file: - - .envs/.holidayrequest - ports: - - "7070:7070" - diff --git a/init.sh b/init.sh index 7fcb27f..e69de29 100644 --- a/init.sh +++ b/init.sh @@ -1,45 +0,0 @@ -#!/bin/bash - -#create init env files: list all services in the array, separeted by space -declare -a arr=("apigateway" "gis" "permission" "chess" "holidayrequest") - -for i in "${arr[@]}" -do - filename="./.envs/.$i" - if [[ ! -e "$filename" ]]; then - mkdir -p ./.envs - touch $filename - fi -done - -# install docker -if docker --version 2>&1 >/dev/null ; then - echo >&2 "docker installed" -else - sudo apt install docker -y -fi - -#install docker-compose -if docker-compose --version 2>&1 >/dev/null ; then - echo >&2 "docker-compose installed" -else - sudo apt install docker-compose -y -fi - -# run docker-compose -declare -i count=$(docker-compose ps | wc -l) -if [ $count -gt 2 ]; then - echo "Restarting services:...." - sudo docker-compose down - sudo docker-compose pull - sudo docker rmi $(docker images -a | grep '' | awk '{print $3}') - sudo docker-compose build --no-cache - sudo docker-compose up -d -else - echo "Starting all services:....." - sudo docker-compose down - sudo docker-compose pull - sudo docker rmi $(docker images -a | grep '' | awk '{print $3}') - sudo docker-compose build --no-cache - sudo docker-compose up -d -fi \ No newline at end of file diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java index 4945b80..e69de29 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -1,61 +0,0 @@ -package io.turntabl.employementprofilingsystem.Controllers; - -import io.swagger.annotations.ApiOperation; -import io.turntabl.employementprofilingsystem.Gmail.Email; -import io.turntabl.employementprofilingsystem.Models.RequestTO; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.BeanPropertyRowMapper; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.web.bind.annotation.*; -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.text.SimpleDateFormat; -import java.util.List; - -@RestController -public class RequestController { - - @Autowired - JdbcTemplate jdbcTemplate; - - @CrossOrigin - @ApiOperation("Make a request") - @PostMapping("/api/v1/request") - public void makeARequest(@RequestBody RequestTO request) { - jdbcTemplate.update("insert into requests(requester_id, request_start_date, request_report_date) values(?,?,?)", - request.getRequester_id(), request.getRequest_start_date(), request.getRequest_report_date()); - - SimpleDateFormat DateFor = new SimpleDateFormat("E, dd MMMM yyyy"); - String startDate = DateFor.format(request.getRequest_start_date()); - String reportDate = DateFor.format(request.getRequest_report_date()); - - try { - Email.requestMessage("isaac.agyen@turntabl.io", request.getFrom() ,"Holiday request", startDate, reportDate, request.getRequester_name()); - } catch (IOException e) { - e.printStackTrace(); - } catch (GeneralSecurityException e) { - e.printStackTrace(); - } - } - - @CrossOrigin - @ApiOperation("Get all requests for requester") - @GetMapping("/api/v1/request/requester/{id}") - public List getRequestByRequesterId(@PathVariable("id") Integer id){ - return this.jdbcTemplate.query( - "select * from requests where requester_id = ?", - new Object[]{id}, - new BeanPropertyRowMapper<>(RequestTO.class) - ); - } - - @CrossOrigin - @ApiOperation("Get all requests") - @GetMapping("/api/v1/requests") - public List getAllRequests(){ - return this.jdbcTemplate.query("select * from requests", - new BeanPropertyRowMapper(RequestTO.class) - ); - } - -} diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java index c5aaed9..e69de29 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java @@ -1,152 +0,0 @@ -package io.turntabl.employementprofilingsystem.Gmail; - -import java.io.IOException; -import java.security.GeneralSecurityException; - -public class Email { - public static void requestMessage(String to, String from, String subject, String request_start_date, String request_report_date, String requester_name) throws IOException, GeneralSecurityException { - String bodyText = "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " Pollstar\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
 \n" + - "
\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
\n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
\n" + - "

Hi there,

\n" + - "

Holiday request by " + requester_name + "

\n" + - "

Start date: " + request_start_date + "

\n" + - "

Report date: " + request_report_date +"

\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
Take Action
\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
\n" + - " Turntabl, Achimota, Mile 7, Sonnidom house\n" + - "
Turntabl.\n" + - "
\n" + - "
\n" + - "
\n" + - "
 
\n" + - " \n" + - " "; - - GmailService.sendMail(from, to, subject, bodyText.toString()); - } -} \ No newline at end of file diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java index defedb1..e69de29 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java @@ -1,98 +0,0 @@ -package io.turntabl.employementprofilingsystem.Gmail; - -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; -import com.google.api.client.http.HttpTransport; -import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson2.JacksonFactory; -import com.google.api.client.util.Base64; -import com.google.api.services.gmail.Gmail; -import com.google.api.services.gmail.GmailScopes; -import com.google.api.services.gmail.model.Message; -import com.google.common.collect.ImmutableList; - -import javax.mail.MessagingException; -import javax.mail.Session; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.util.List; -import java.util.Properties; - -public class GmailService { - private static final String APPLICATION_NAME = "GmailService"; - private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); - - private static final List SCOPES = ImmutableList.of(GmailScopes.GMAIL_SEND, GmailScopes.GMAIL_COMPOSE); - private static final String CREDENTIALS_FILE_PATH = "credentials.json"; - - public static void sendMail(String from, String to, String subject, String bodyText) throws IOException, GeneralSecurityException { - Gmail service = getGmail(); - System.out.println(service.getApplicationName()); - System.out.println(service.getBaseUrl()); - System.out.println(service.users().toString()); - - String user = from; - try { - service.users().messages().send(user, - createMessageWithEmail(createEmail(to, from, subject, bodyText)) - ).execute(); - } catch (MessagingException e) { - e.printStackTrace(); - } - } - - public static Gmail getGmail() throws GeneralSecurityException, IOException { - HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - - GoogleCredential gcFromJson = GoogleCredential - .fromStream(new FileInputStream(CREDENTIALS_FILE_PATH)) - .createScoped(SCOPES); - - GoogleCredential credential = new GoogleCredential.Builder() - .setTransport(gcFromJson.getTransport()) - .setJsonFactory(gcFromJson.getJsonFactory()) - .setServiceAccountId(gcFromJson.getServiceAccountId()) - .setServiceAccountUser( "dawud.ismail@turntabl.io") - .setServiceAccountPrivateKey(gcFromJson.getServiceAccountPrivateKey()) - .setServiceAccountScopes(gcFromJson.getServiceAccountScopes()) - .setTokenServerEncodedUrl(gcFromJson.getTokenServerEncodedUrl()) - .build(); - boolean token = credential.refreshToken(); - System.out.println(token); - return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) - .setApplicationName(APPLICATION_NAME) - .build(); - } - - private static Message createMessageWithEmail(MimeMessage emailContent) - throws MessagingException, IOException { - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - emailContent.writeTo(buffer); - byte[] bytes = buffer.toByteArray(); - String encodedEmail = Base64.encodeBase64URLSafeString(bytes); - Message message = new Message(); - message.setRaw(encodedEmail); - return message; - } - - private static MimeMessage createEmail(String to, - String from, - String subject, - String bodyText) throws MessagingException { - Properties props = new Properties(); - Session session = Session.getDefaultInstance(props, null); - - MimeMessage email = new MimeMessage(session); - - email.setFrom(new InternetAddress( from )); - email.addRecipient(javax.mail.Message.RecipientType.TO, - new InternetAddress(to)); - email.setSubject(subject); - email.setText(bodyText, "UTF-8", "html"); - return email; - } -} diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java index 91dfa9b..e69de29 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java @@ -1,95 +0,0 @@ -package io.turntabl.employementprofilingsystem.Models; - -import java.util.Date; - -public class RequestTO { - private int request_id; - private int requester_id; - private Date request_start_date; - private Date request_report_date; - private int request_status_id; - private String from; - private String requester_name; - - public RequestTO() { - } - - public RequestTO(int request_id, int requester_id, Date request_start_date, Date request_report_date, int request_status_id, String from, String requester_name) { - this.request_id = request_id; - this.requester_id = requester_id; - this.request_start_date = request_start_date; - this.request_report_date = request_report_date; - this.request_status_id = request_status_id; - this.from = from; - this.requester_name = requester_name; - } - - public int getRequest_id() { - return request_id; - } - - public void setRequest_id(int request_id) { - this.request_id = request_id; - } - - public int getRequester_id() { - return requester_id; - } - - public void setRequester_id(int requester_id) { - this.requester_id = requester_id; - } - - public Date getRequest_start_date() { - return request_start_date; - } - - public void setRequest_start_date(Date request_start_date) { - this.request_start_date = request_start_date; - } - - public Date getRequest_report_date() { - return request_report_date; - } - - public void setRequest_report_date(Date request_report_date) { - this.request_report_date = request_report_date; - } - - public int getRequest_status_id() { - return request_status_id; - } - - public void setRequest_status_id(int request_status_id) { - this.request_status_id = request_status_id; - } - - public String getFrom() { - return from; - } - - public void setFrom(String from) { - this.from = from; - } - - public String getRequester_name() { - return requester_name; - } - - public void setRequester_name(String requester_name) { - this.requester_name = requester_name; - } - - @Override - public String toString() { - return "RequestTO{" + - "request_id=" + request_id + - ", requester_id=" + requester_id + - ", request_start_date=" + request_start_date + - ", request_report_date=" + request_report_date + - ", request_status_id=" + request_status_id + - ", from='" + from + '\'' + - ", requester_name='" + requester_name + '\'' + - '}'; - } -} From 2f320f807a3a0c5e21a9d96d6c2a46e2df4e8cea Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 10:44:13 +0000 Subject: [PATCH 06/18] added requestController --- .../Controllers/RequestController.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java index e69de29..4945b80 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -0,0 +1,61 @@ +package io.turntabl.employementprofilingsystem.Controllers; + +import io.swagger.annotations.ApiOperation; +import io.turntabl.employementprofilingsystem.Gmail.Email; +import io.turntabl.employementprofilingsystem.Models.RequestTO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.BeanPropertyRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.*; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.text.SimpleDateFormat; +import java.util.List; + +@RestController +public class RequestController { + + @Autowired + JdbcTemplate jdbcTemplate; + + @CrossOrigin + @ApiOperation("Make a request") + @PostMapping("/api/v1/request") + public void makeARequest(@RequestBody RequestTO request) { + jdbcTemplate.update("insert into requests(requester_id, request_start_date, request_report_date) values(?,?,?)", + request.getRequester_id(), request.getRequest_start_date(), request.getRequest_report_date()); + + SimpleDateFormat DateFor = new SimpleDateFormat("E, dd MMMM yyyy"); + String startDate = DateFor.format(request.getRequest_start_date()); + String reportDate = DateFor.format(request.getRequest_report_date()); + + try { + Email.requestMessage("isaac.agyen@turntabl.io", request.getFrom() ,"Holiday request", startDate, reportDate, request.getRequester_name()); + } catch (IOException e) { + e.printStackTrace(); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + } + } + + @CrossOrigin + @ApiOperation("Get all requests for requester") + @GetMapping("/api/v1/request/requester/{id}") + public List getRequestByRequesterId(@PathVariable("id") Integer id){ + return this.jdbcTemplate.query( + "select * from requests where requester_id = ?", + new Object[]{id}, + new BeanPropertyRowMapper<>(RequestTO.class) + ); + } + + @CrossOrigin + @ApiOperation("Get all requests") + @GetMapping("/api/v1/requests") + public List getAllRequests(){ + return this.jdbcTemplate.query("select * from requests", + new BeanPropertyRowMapper(RequestTO.class) + ); + } + +} From de71051b091e57e79069764cc3598ddb6ff3ecf8 Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 10:44:45 +0000 Subject: [PATCH 07/18] added email --- .../Gmail/Email.java | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java index e69de29..c5aaed9 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java @@ -0,0 +1,152 @@ +package io.turntabl.employementprofilingsystem.Gmail; + +import java.io.IOException; +import java.security.GeneralSecurityException; + +public class Email { + public static void requestMessage(String to, String from, String subject, String request_start_date, String request_report_date, String requester_name) throws IOException, GeneralSecurityException { + String bodyText = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " Pollstar\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
 \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + "

Hi there,

\n" + + "

Holiday request by " + requester_name + "

\n" + + "

Start date: " + request_start_date + "

\n" + + "

Report date: " + request_report_date +"

\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
Take Action
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " Turntabl, Achimota, Mile 7, Sonnidom house\n" + + "
Turntabl.\n" + + "
\n" + + "
\n" + + "
\n" + + "
 
\n" + + " \n" + + " "; + + GmailService.sendMail(from, to, subject, bodyText.toString()); + } +} \ No newline at end of file From c844e7f4c344a381f2e4288e2ab67693fa48590f Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 10:45:37 +0000 Subject: [PATCH 08/18] added gmail service --- .../Gmail/GmailService.java | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java index e69de29..defedb1 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java @@ -0,0 +1,98 @@ +package io.turntabl.employementprofilingsystem.Gmail; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.Base64; +import com.google.api.services.gmail.Gmail; +import com.google.api.services.gmail.GmailScopes; +import com.google.api.services.gmail.model.Message; +import com.google.common.collect.ImmutableList; + +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.List; +import java.util.Properties; + +public class GmailService { + private static final String APPLICATION_NAME = "GmailService"; + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + + private static final List SCOPES = ImmutableList.of(GmailScopes.GMAIL_SEND, GmailScopes.GMAIL_COMPOSE); + private static final String CREDENTIALS_FILE_PATH = "credentials.json"; + + public static void sendMail(String from, String to, String subject, String bodyText) throws IOException, GeneralSecurityException { + Gmail service = getGmail(); + System.out.println(service.getApplicationName()); + System.out.println(service.getBaseUrl()); + System.out.println(service.users().toString()); + + String user = from; + try { + service.users().messages().send(user, + createMessageWithEmail(createEmail(to, from, subject, bodyText)) + ).execute(); + } catch (MessagingException e) { + e.printStackTrace(); + } + } + + public static Gmail getGmail() throws GeneralSecurityException, IOException { + HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + + GoogleCredential gcFromJson = GoogleCredential + .fromStream(new FileInputStream(CREDENTIALS_FILE_PATH)) + .createScoped(SCOPES); + + GoogleCredential credential = new GoogleCredential.Builder() + .setTransport(gcFromJson.getTransport()) + .setJsonFactory(gcFromJson.getJsonFactory()) + .setServiceAccountId(gcFromJson.getServiceAccountId()) + .setServiceAccountUser( "dawud.ismail@turntabl.io") + .setServiceAccountPrivateKey(gcFromJson.getServiceAccountPrivateKey()) + .setServiceAccountScopes(gcFromJson.getServiceAccountScopes()) + .setTokenServerEncodedUrl(gcFromJson.getTokenServerEncodedUrl()) + .build(); + boolean token = credential.refreshToken(); + System.out.println(token); + return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) + .setApplicationName(APPLICATION_NAME) + .build(); + } + + private static Message createMessageWithEmail(MimeMessage emailContent) + throws MessagingException, IOException { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + emailContent.writeTo(buffer); + byte[] bytes = buffer.toByteArray(); + String encodedEmail = Base64.encodeBase64URLSafeString(bytes); + Message message = new Message(); + message.setRaw(encodedEmail); + return message; + } + + private static MimeMessage createEmail(String to, + String from, + String subject, + String bodyText) throws MessagingException { + Properties props = new Properties(); + Session session = Session.getDefaultInstance(props, null); + + MimeMessage email = new MimeMessage(session); + + email.setFrom(new InternetAddress( from )); + email.addRecipient(javax.mail.Message.RecipientType.TO, + new InternetAddress(to)); + email.setSubject(subject); + email.setText(bodyText, "UTF-8", "html"); + return email; + } +} From 3ccd12bbc5a0df52951e609aaaffbbf6df79ec9c Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 10:46:04 +0000 Subject: [PATCH 09/18] added RequestTo --- .../Models/RequestTO.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java index e69de29..91dfa9b 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java @@ -0,0 +1,95 @@ +package io.turntabl.employementprofilingsystem.Models; + +import java.util.Date; + +public class RequestTO { + private int request_id; + private int requester_id; + private Date request_start_date; + private Date request_report_date; + private int request_status_id; + private String from; + private String requester_name; + + public RequestTO() { + } + + public RequestTO(int request_id, int requester_id, Date request_start_date, Date request_report_date, int request_status_id, String from, String requester_name) { + this.request_id = request_id; + this.requester_id = requester_id; + this.request_start_date = request_start_date; + this.request_report_date = request_report_date; + this.request_status_id = request_status_id; + this.from = from; + this.requester_name = requester_name; + } + + public int getRequest_id() { + return request_id; + } + + public void setRequest_id(int request_id) { + this.request_id = request_id; + } + + public int getRequester_id() { + return requester_id; + } + + public void setRequester_id(int requester_id) { + this.requester_id = requester_id; + } + + public Date getRequest_start_date() { + return request_start_date; + } + + public void setRequest_start_date(Date request_start_date) { + this.request_start_date = request_start_date; + } + + public Date getRequest_report_date() { + return request_report_date; + } + + public void setRequest_report_date(Date request_report_date) { + this.request_report_date = request_report_date; + } + + public int getRequest_status_id() { + return request_status_id; + } + + public void setRequest_status_id(int request_status_id) { + this.request_status_id = request_status_id; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getRequester_name() { + return requester_name; + } + + public void setRequester_name(String requester_name) { + this.requester_name = requester_name; + } + + @Override + public String toString() { + return "RequestTO{" + + "request_id=" + request_id + + ", requester_id=" + requester_id + + ", request_start_date=" + request_start_date + + ", request_report_date=" + request_report_date + + ", request_status_id=" + request_status_id + + ", from='" + from + '\'' + + ", requester_name='" + requester_name + '\'' + + '}'; + } +} From 2c771cac2468e4b86909d510e8a065ffddf2b88f Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 10:47:39 +0000 Subject: [PATCH 10/18] added docker-compose.yml --- docker-compose.yml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index e69de29..20da902 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -0,0 +1,33 @@ +version: '3' +services: + apigateway: + image: "idawud/turntablapigateway:latest" + env_file: + - .envs/.apigateway + ports: + - "80:8081" + permission: + image: "idawud/accperm:v3" + env_file: + - .envs/.permission + ports: + - "5000:5000" + gis: + image: "isammyk/gis-docker:latest" + env_file: + - .envs/.gis + ports: + - "5004:5004" + chess: + image: "francisbilla/chess:latest" + env_file: + - .envs/.chess + ports: + - "8080:8080" + holidayrequest: + image: " billalidocker/employee-service:latest" + env_file: + - .envs/.holidayrequest + ports: + - "7070:7070" + From 195724da0eaed22a4d1af47d41f4f7422d3e6b31 Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 10:48:31 +0000 Subject: [PATCH 11/18] added files on dockerfile --- Dockerfile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Dockerfile b/Dockerfile index e69de29..85cbe21 100644 --- a/Dockerfile +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM adoptopenjdk:11-jre-hotspot +COPY build/libs/employement-profiling-system-0.0.1-SNAPSHOT.jar app.jar +RUN chmod 777 app.jar + +ENTRYPOINT [ "java", "-jar", "app.jar" ] \ No newline at end of file From 5eca1cc6e2db66d9686f175bb61c595b43f272bc Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 10:49:01 +0000 Subject: [PATCH 12/18] added files into init.sh --- init.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/init.sh b/init.sh index e69de29..7fcb27f 100644 --- a/init.sh +++ b/init.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +#create init env files: list all services in the array, separeted by space +declare -a arr=("apigateway" "gis" "permission" "chess" "holidayrequest") + +for i in "${arr[@]}" +do + filename="./.envs/.$i" + if [[ ! -e "$filename" ]]; then + mkdir -p ./.envs + touch $filename + fi +done + +# install docker +if docker --version 2>&1 >/dev/null ; then + echo >&2 "docker installed" +else + sudo apt install docker -y +fi + +#install docker-compose +if docker-compose --version 2>&1 >/dev/null ; then + echo >&2 "docker-compose installed" +else + sudo apt install docker-compose -y +fi + +# run docker-compose +declare -i count=$(docker-compose ps | wc -l) +if [ $count -gt 2 ]; then + echo "Restarting services:...." + sudo docker-compose down + sudo docker-compose pull + sudo docker rmi $(docker images -a | grep '' | awk '{print $3}') + sudo docker-compose build --no-cache + sudo docker-compose up -d +else + echo "Starting all services:....." + sudo docker-compose down + sudo docker-compose pull + sudo docker rmi $(docker images -a | grep '' | awk '{print $3}') + sudo docker-compose build --no-cache + sudo docker-compose up -d +fi \ No newline at end of file From 5f5091c2b5ff07bf9b65d1e0116b93995a23c2ca Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 10:59:55 +0000 Subject: [PATCH 13/18] added holiday sql files --- db/holiday/holiday.sql | 21 +++++++++++++++++++++ db/holiday/requestStatusInsertion.sql | 3 +++ db/holiday/requestsTableInsertion.sql | 3 +++ 3 files changed, 27 insertions(+) create mode 100644 db/holiday/holiday.sql create mode 100644 db/holiday/requestStatusInsertion.sql create mode 100644 db/holiday/requestsTableInsertion.sql diff --git a/db/holiday/holiday.sql b/db/holiday/holiday.sql new file mode 100644 index 0000000..e6da465 --- /dev/null +++ b/db/holiday/holiday.sql @@ -0,0 +1,21 @@ +--- DATABASE FOR HOLIDAY ME --- + +DROP TABLE IF EXISTS requests cascade; +DROP TABLE IF EXISTS request_status cascade; + +--- REQUEST STATUS --- + +CREATE TABLE request_status( + request_status_id serial primary key NOT NULL, + req_status varchar(10) +); + +--- REQUESTS TABLE --- + +CREATE TABLE requests( + request_id serial primary key NOT NULL, + requester_id int references employee(employee_id) NOT NULL, + request_start_date date NOT NULL, + request_report_date date NOT NULL, + request_status_id int references request_status(request_status_id) NOT NULL default 1 +); \ No newline at end of file diff --git a/db/holiday/requestStatusInsertion.sql b/db/holiday/requestStatusInsertion.sql new file mode 100644 index 0000000..e1072a8 --- /dev/null +++ b/db/holiday/requestStatusInsertion.sql @@ -0,0 +1,3 @@ +INSERT INTO request_status(req_status) VALUES ('PENDING'); +INSERT INTO request_status(req_status) VALUES ('DECLINED'); +INSERT INTO request_status(req_status) VALUES ('APPROVED'); diff --git a/db/holiday/requestsTableInsertion.sql b/db/holiday/requestsTableInsertion.sql new file mode 100644 index 0000000..d036955 --- /dev/null +++ b/db/holiday/requestsTableInsertion.sql @@ -0,0 +1,3 @@ +INSERT INTO requests(requester_id, request_start_date, request_report_date, request_status_id) values(1,'2020-02-20', '2020-02-25', 2); +INSERT INTO requests(requester_id, request_start_date, request_report_date, request_status_id) values(1,'2020-02-20', '2020-02-25', 3); +INSERT INTO requests(requester_id, request_start_date, request_report_date, request_status_id) values(1,'2020-02-20', '2020-02-25', 1); From ec6fa0f500bdf18d654031f819cbedc30a0112ca Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 11:07:14 +0000 Subject: [PATCH 14/18] mofified --- Dockerfile | 5 + db/holiday/holiday.sql | 21 +++ db/holiday/requestStatusInsertion.sql | 3 + db/holiday/requestsTableInsertion.sql | 3 + docker-compose.yml | 33 ++++ init.sh | 45 ++++++ .../Controllers/RequestController.java | 61 +++++++ .../Gmail/Email.java | 152 ++++++++++++++++++ .../Gmail/GmailService.java | 98 +++++++++++ .../Models/RequestTO.java | 95 +++++++++++ 10 files changed, 516 insertions(+) create mode 100644 db/holiday/holiday.sql create mode 100644 db/holiday/requestStatusInsertion.sql create mode 100644 db/holiday/requestsTableInsertion.sql diff --git a/Dockerfile b/Dockerfile index e69de29..85cbe21 100644 --- a/Dockerfile +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM adoptopenjdk:11-jre-hotspot +COPY build/libs/employement-profiling-system-0.0.1-SNAPSHOT.jar app.jar +RUN chmod 777 app.jar + +ENTRYPOINT [ "java", "-jar", "app.jar" ] \ No newline at end of file diff --git a/db/holiday/holiday.sql b/db/holiday/holiday.sql new file mode 100644 index 0000000..e6da465 --- /dev/null +++ b/db/holiday/holiday.sql @@ -0,0 +1,21 @@ +--- DATABASE FOR HOLIDAY ME --- + +DROP TABLE IF EXISTS requests cascade; +DROP TABLE IF EXISTS request_status cascade; + +--- REQUEST STATUS --- + +CREATE TABLE request_status( + request_status_id serial primary key NOT NULL, + req_status varchar(10) +); + +--- REQUESTS TABLE --- + +CREATE TABLE requests( + request_id serial primary key NOT NULL, + requester_id int references employee(employee_id) NOT NULL, + request_start_date date NOT NULL, + request_report_date date NOT NULL, + request_status_id int references request_status(request_status_id) NOT NULL default 1 +); \ No newline at end of file diff --git a/db/holiday/requestStatusInsertion.sql b/db/holiday/requestStatusInsertion.sql new file mode 100644 index 0000000..e1072a8 --- /dev/null +++ b/db/holiday/requestStatusInsertion.sql @@ -0,0 +1,3 @@ +INSERT INTO request_status(req_status) VALUES ('PENDING'); +INSERT INTO request_status(req_status) VALUES ('DECLINED'); +INSERT INTO request_status(req_status) VALUES ('APPROVED'); diff --git a/db/holiday/requestsTableInsertion.sql b/db/holiday/requestsTableInsertion.sql new file mode 100644 index 0000000..d036955 --- /dev/null +++ b/db/holiday/requestsTableInsertion.sql @@ -0,0 +1,3 @@ +INSERT INTO requests(requester_id, request_start_date, request_report_date, request_status_id) values(1,'2020-02-20', '2020-02-25', 2); +INSERT INTO requests(requester_id, request_start_date, request_report_date, request_status_id) values(1,'2020-02-20', '2020-02-25', 3); +INSERT INTO requests(requester_id, request_start_date, request_report_date, request_status_id) values(1,'2020-02-20', '2020-02-25', 1); diff --git a/docker-compose.yml b/docker-compose.yml index e69de29..20da902 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -0,0 +1,33 @@ +version: '3' +services: + apigateway: + image: "idawud/turntablapigateway:latest" + env_file: + - .envs/.apigateway + ports: + - "80:8081" + permission: + image: "idawud/accperm:v3" + env_file: + - .envs/.permission + ports: + - "5000:5000" + gis: + image: "isammyk/gis-docker:latest" + env_file: + - .envs/.gis + ports: + - "5004:5004" + chess: + image: "francisbilla/chess:latest" + env_file: + - .envs/.chess + ports: + - "8080:8080" + holidayrequest: + image: " billalidocker/employee-service:latest" + env_file: + - .envs/.holidayrequest + ports: + - "7070:7070" + diff --git a/init.sh b/init.sh index e69de29..7fcb27f 100644 --- a/init.sh +++ b/init.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +#create init env files: list all services in the array, separeted by space +declare -a arr=("apigateway" "gis" "permission" "chess" "holidayrequest") + +for i in "${arr[@]}" +do + filename="./.envs/.$i" + if [[ ! -e "$filename" ]]; then + mkdir -p ./.envs + touch $filename + fi +done + +# install docker +if docker --version 2>&1 >/dev/null ; then + echo >&2 "docker installed" +else + sudo apt install docker -y +fi + +#install docker-compose +if docker-compose --version 2>&1 >/dev/null ; then + echo >&2 "docker-compose installed" +else + sudo apt install docker-compose -y +fi + +# run docker-compose +declare -i count=$(docker-compose ps | wc -l) +if [ $count -gt 2 ]; then + echo "Restarting services:...." + sudo docker-compose down + sudo docker-compose pull + sudo docker rmi $(docker images -a | grep '' | awk '{print $3}') + sudo docker-compose build --no-cache + sudo docker-compose up -d +else + echo "Starting all services:....." + sudo docker-compose down + sudo docker-compose pull + sudo docker rmi $(docker images -a | grep '' | awk '{print $3}') + sudo docker-compose build --no-cache + sudo docker-compose up -d +fi \ No newline at end of file diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java index e69de29..4945b80 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -0,0 +1,61 @@ +package io.turntabl.employementprofilingsystem.Controllers; + +import io.swagger.annotations.ApiOperation; +import io.turntabl.employementprofilingsystem.Gmail.Email; +import io.turntabl.employementprofilingsystem.Models.RequestTO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.BeanPropertyRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.*; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.text.SimpleDateFormat; +import java.util.List; + +@RestController +public class RequestController { + + @Autowired + JdbcTemplate jdbcTemplate; + + @CrossOrigin + @ApiOperation("Make a request") + @PostMapping("/api/v1/request") + public void makeARequest(@RequestBody RequestTO request) { + jdbcTemplate.update("insert into requests(requester_id, request_start_date, request_report_date) values(?,?,?)", + request.getRequester_id(), request.getRequest_start_date(), request.getRequest_report_date()); + + SimpleDateFormat DateFor = new SimpleDateFormat("E, dd MMMM yyyy"); + String startDate = DateFor.format(request.getRequest_start_date()); + String reportDate = DateFor.format(request.getRequest_report_date()); + + try { + Email.requestMessage("isaac.agyen@turntabl.io", request.getFrom() ,"Holiday request", startDate, reportDate, request.getRequester_name()); + } catch (IOException e) { + e.printStackTrace(); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + } + } + + @CrossOrigin + @ApiOperation("Get all requests for requester") + @GetMapping("/api/v1/request/requester/{id}") + public List getRequestByRequesterId(@PathVariable("id") Integer id){ + return this.jdbcTemplate.query( + "select * from requests where requester_id = ?", + new Object[]{id}, + new BeanPropertyRowMapper<>(RequestTO.class) + ); + } + + @CrossOrigin + @ApiOperation("Get all requests") + @GetMapping("/api/v1/requests") + public List getAllRequests(){ + return this.jdbcTemplate.query("select * from requests", + new BeanPropertyRowMapper(RequestTO.class) + ); + } + +} diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java index e69de29..c5aaed9 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/Email.java @@ -0,0 +1,152 @@ +package io.turntabl.employementprofilingsystem.Gmail; + +import java.io.IOException; +import java.security.GeneralSecurityException; + +public class Email { + public static void requestMessage(String to, String from, String subject, String request_start_date, String request_report_date, String requester_name) throws IOException, GeneralSecurityException { + String bodyText = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " Pollstar\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
 \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + "

Hi there,

\n" + + "

Holiday request by " + requester_name + "

\n" + + "

Start date: " + request_start_date + "

\n" + + "

Report date: " + request_report_date +"

\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
Take Action
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " Turntabl, Achimota, Mile 7, Sonnidom house\n" + + "
Turntabl.\n" + + "
\n" + + "
\n" + + "
\n" + + "
 
\n" + + " \n" + + " "; + + GmailService.sendMail(from, to, subject, bodyText.toString()); + } +} \ No newline at end of file diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java index e69de29..defedb1 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java @@ -0,0 +1,98 @@ +package io.turntabl.employementprofilingsystem.Gmail; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.Base64; +import com.google.api.services.gmail.Gmail; +import com.google.api.services.gmail.GmailScopes; +import com.google.api.services.gmail.model.Message; +import com.google.common.collect.ImmutableList; + +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.List; +import java.util.Properties; + +public class GmailService { + private static final String APPLICATION_NAME = "GmailService"; + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + + private static final List SCOPES = ImmutableList.of(GmailScopes.GMAIL_SEND, GmailScopes.GMAIL_COMPOSE); + private static final String CREDENTIALS_FILE_PATH = "credentials.json"; + + public static void sendMail(String from, String to, String subject, String bodyText) throws IOException, GeneralSecurityException { + Gmail service = getGmail(); + System.out.println(service.getApplicationName()); + System.out.println(service.getBaseUrl()); + System.out.println(service.users().toString()); + + String user = from; + try { + service.users().messages().send(user, + createMessageWithEmail(createEmail(to, from, subject, bodyText)) + ).execute(); + } catch (MessagingException e) { + e.printStackTrace(); + } + } + + public static Gmail getGmail() throws GeneralSecurityException, IOException { + HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + + GoogleCredential gcFromJson = GoogleCredential + .fromStream(new FileInputStream(CREDENTIALS_FILE_PATH)) + .createScoped(SCOPES); + + GoogleCredential credential = new GoogleCredential.Builder() + .setTransport(gcFromJson.getTransport()) + .setJsonFactory(gcFromJson.getJsonFactory()) + .setServiceAccountId(gcFromJson.getServiceAccountId()) + .setServiceAccountUser( "dawud.ismail@turntabl.io") + .setServiceAccountPrivateKey(gcFromJson.getServiceAccountPrivateKey()) + .setServiceAccountScopes(gcFromJson.getServiceAccountScopes()) + .setTokenServerEncodedUrl(gcFromJson.getTokenServerEncodedUrl()) + .build(); + boolean token = credential.refreshToken(); + System.out.println(token); + return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) + .setApplicationName(APPLICATION_NAME) + .build(); + } + + private static Message createMessageWithEmail(MimeMessage emailContent) + throws MessagingException, IOException { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + emailContent.writeTo(buffer); + byte[] bytes = buffer.toByteArray(); + String encodedEmail = Base64.encodeBase64URLSafeString(bytes); + Message message = new Message(); + message.setRaw(encodedEmail); + return message; + } + + private static MimeMessage createEmail(String to, + String from, + String subject, + String bodyText) throws MessagingException { + Properties props = new Properties(); + Session session = Session.getDefaultInstance(props, null); + + MimeMessage email = new MimeMessage(session); + + email.setFrom(new InternetAddress( from )); + email.addRecipient(javax.mail.Message.RecipientType.TO, + new InternetAddress(to)); + email.setSubject(subject); + email.setText(bodyText, "UTF-8", "html"); + return email; + } +} diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java index e69de29..91dfa9b 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java @@ -0,0 +1,95 @@ +package io.turntabl.employementprofilingsystem.Models; + +import java.util.Date; + +public class RequestTO { + private int request_id; + private int requester_id; + private Date request_start_date; + private Date request_report_date; + private int request_status_id; + private String from; + private String requester_name; + + public RequestTO() { + } + + public RequestTO(int request_id, int requester_id, Date request_start_date, Date request_report_date, int request_status_id, String from, String requester_name) { + this.request_id = request_id; + this.requester_id = requester_id; + this.request_start_date = request_start_date; + this.request_report_date = request_report_date; + this.request_status_id = request_status_id; + this.from = from; + this.requester_name = requester_name; + } + + public int getRequest_id() { + return request_id; + } + + public void setRequest_id(int request_id) { + this.request_id = request_id; + } + + public int getRequester_id() { + return requester_id; + } + + public void setRequester_id(int requester_id) { + this.requester_id = requester_id; + } + + public Date getRequest_start_date() { + return request_start_date; + } + + public void setRequest_start_date(Date request_start_date) { + this.request_start_date = request_start_date; + } + + public Date getRequest_report_date() { + return request_report_date; + } + + public void setRequest_report_date(Date request_report_date) { + this.request_report_date = request_report_date; + } + + public int getRequest_status_id() { + return request_status_id; + } + + public void setRequest_status_id(int request_status_id) { + this.request_status_id = request_status_id; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getRequester_name() { + return requester_name; + } + + public void setRequester_name(String requester_name) { + this.requester_name = requester_name; + } + + @Override + public String toString() { + return "RequestTO{" + + "request_id=" + request_id + + ", requester_id=" + requester_id + + ", request_start_date=" + request_start_date + + ", request_report_date=" + request_report_date + + ", request_status_id=" + request_status_id + + ", from='" + from + '\'' + + ", requester_name='" + requester_name + '\'' + + '}'; + } +} From 035ce69d1c6ac9028e27abdf34fa2acec0108beb Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 15:35:07 +0000 Subject: [PATCH 15/18] modified api operation --- .../Controllers/RequestController.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java index 5f55053..870b9f7 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -19,7 +19,7 @@ public class RequestController { JdbcTemplate jdbcTemplate; @CrossOrigin - @ApiOperation("Make a request") + @ApiOperation("Make holiday request") @PostMapping("/api/v1/request") public void makeARequest(@RequestBody RequestTO request) { jdbcTemplate.update("insert into requests(requester_id, request_start_date, request_report_date) values(?,?,?)", @@ -39,7 +39,7 @@ public void makeARequest(@RequestBody RequestTO request) { } @CrossOrigin - @ApiOperation("Get all requests for requester") + @ApiOperation("Get all holiday requests by a requester") @GetMapping("/api/v1/request/requester/{id}") public List getRequestByRequesterId(@PathVariable("id") Integer id) { @@ -52,7 +52,7 @@ public List getRequestByRequesterId(@PathVariable("id") Integer id) { } @CrossOrigin - @ApiOperation("Get all requests") + @ApiOperation("Get all holiday requests") @GetMapping("/api/v1/requests") public List getAllRequests() { @@ -63,14 +63,14 @@ public List getAllRequests() { } @CrossOrigin - @ApiOperation("Accept Request") + @ApiOperation("Accept holiday Request") @PutMapping("/api/v1/requests/approve/{id}") public void approveRequest(@PathVariable("id") Integer request_id) { this.jdbcTemplate.update("update requests set request_status_id = 3 where request_status_id = 1 and request_id = ?", request_id); } @CrossOrigin - @ApiOperation("Decline Request") + @ApiOperation("Decline holiday Request") @PutMapping("/api/v1/requests/decline/{id}") public void declineRequest(@PathVariable("id") Integer request_id) { this.jdbcTemplate.update("update requests set request_status_id = 2 where request_status_id = 1 and request_id = ?", request_id); From e6d4a8bdf3808542b4cb45e811ef66d0bb048d29 Mon Sep 17 00:00:00 2001 From: bill-n Date: Tue, 25 Feb 2020 15:44:48 +0000 Subject: [PATCH 16/18] modified file --- .../Controllers/RequestController.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java index 870b9f7..6da48ad 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -19,7 +19,7 @@ public class RequestController { JdbcTemplate jdbcTemplate; @CrossOrigin - @ApiOperation("Make holiday request") + @ApiOperation("Make a holiday request") @PostMapping("/api/v1/request") public void makeARequest(@RequestBody RequestTO request) { jdbcTemplate.update("insert into requests(requester_id, request_start_date, request_report_date) values(?,?,?)", @@ -39,7 +39,7 @@ public void makeARequest(@RequestBody RequestTO request) { } @CrossOrigin - @ApiOperation("Get all holiday requests by a requester") + @ApiOperation("Get all holiday requests for requester") @GetMapping("/api/v1/request/requester/{id}") public List getRequestByRequesterId(@PathVariable("id") Integer id) { @@ -63,16 +63,17 @@ public List getAllRequests() { } @CrossOrigin - @ApiOperation("Accept holiday Request") + @ApiOperation("Accept holiday request") @PutMapping("/api/v1/requests/approve/{id}") public void approveRequest(@PathVariable("id") Integer request_id) { this.jdbcTemplate.update("update requests set request_status_id = 3 where request_status_id = 1 and request_id = ?", request_id); } @CrossOrigin - @ApiOperation("Decline holiday Request") + @ApiOperation("Decline holiday request") @PutMapping("/api/v1/requests/decline/{id}") public void declineRequest(@PathVariable("id") Integer request_id) { this.jdbcTemplate.update("update requests set request_status_id = 2 where request_status_id = 1 and request_id = ?", request_id); } } + From 035447f7445c86f90578c3a68134503ab6bfcc52 Mon Sep 17 00:00:00 2001 From: agyen Date: Thu, 27 Feb 2020 10:14:30 +0000 Subject: [PATCH 17/18] oidc --- build.gradle | 3 + db/holiday.sql | 42 +++++++++++ public_key.pem | 0 .../Controllers/RequestController.java | 75 ++++++++++++++++++- ...EmployementProfilingSystemApplication.java | 1 + .../Models/RequestTO.java | 13 +++- src/main/resources/application.properties | 10 ++- 7 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 db/holiday.sql create mode 100644 public_key.pem diff --git a/build.gradle b/build.gradle index 457756b..27f8b26 100644 --- a/build.gradle +++ b/build.gradle @@ -39,6 +39,9 @@ dependencies { compile 'com.google.apis:google-api-services-gmail:v1-rev83-1.23.0' compile group: 'javax.mail', name: 'mail', version: '1.4' compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1' + compile 'io.jsonwebtoken:jjwt-api:0.10.7' + runtime 'io.jsonwebtoken:jjwt-impl:0.10.7' + implementation 'io.fusionauth:fusionauth-jwt:3.1.6' } test { diff --git a/db/holiday.sql b/db/holiday.sql new file mode 100644 index 0000000..6a10ecf --- /dev/null +++ b/db/holiday.sql @@ -0,0 +1,42 @@ +DROP TABLE IF EXISTS employee cascade; + +CREATE TABLE employee ( + employee_id serial primary key, + employee_firstName text not null, + employee_lastName text not null, + employee_phoneNumber text, + employee_email text not null, + employee_address text, + employee_role text, + employee_dev_level text, + employee_hire_date DATE, + employee_onLeave BOOLEAN, + employee_gender text, + employee_status text +); + +--- DATABASE FOR HOLIDAY ME --- + +DROP TABLE IF EXISTS requests cascade; +DROP TABLE IF EXISTS request_status cascade; + +--- REQUEST STATUS --- + +CREATE TABLE request_status( + request_status_id serial primary key NOT NULL, + req_status varchar(10) +); + +INSERT INTO request_status(request_status_id, req_status) VALUES (1, 'PENDING'); +INSERT INTO request_status(request_status_id, req_status) VALUES (2, 'DECLINED'); +INSERT INTO request_status(request_status_id, req_status) VALUES (3, 'APPROVED'); + +--- REQUESTS TABLE --- + +CREATE TABLE requests( + request_id serial primary key NOT NULL, + requester_id int references employee(employee_id) NOT NULL, + request_start_date date NOT NULL, + request_report_date date NOT NULL, + request_status_id int references request_status(request_status_id) NOT NULL default 1 +); \ No newline at end of file diff --git a/public_key.pem b/public_key.pem new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java index 289756c..5359310 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -1,16 +1,28 @@ package io.turntabl.employementprofilingsystem.Controllers; +import io.fusionauth.jwt.JWTException; +import io.fusionauth.jwt.Verifier; +import io.fusionauth.jwt.domain.JWT; +import io.fusionauth.jwt.rsa.RSAVerifier; +import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import io.turntabl.employementprofilingsystem.Models.AddEmployee; import io.turntabl.employementprofilingsystem.Models.RequestTO; +import io.turntabl.employementprofilingsystem.Transfers.Employee; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.web.bind.annotation.*; import java.io.IOException; +import java.nio.file.Paths; import java.security.GeneralSecurityException; import java.text.SimpleDateFormat; +import java.util.HashMap; import java.util.List; +import java.util.Map; +@Api @RestController public class RequestController { @@ -30,8 +42,7 @@ public void makeARequest(@RequestBody RequestTO request) { @GetMapping("/api/v1/request/requester/{id}") public List getRequestByRequesterId(@PathVariable("id") Integer id) { return this.jdbcTemplate.query( - "select select request_start_date, request_report_date, request_status.req_status from requests inner join request_status on " + - "requests.request_status_id = request_status.request_status_id where requester_id = ?", + " select request_start_date, request_report_date, request_status.req_status from requests inner join request_status on requests.request_status_id = request_status.request_status_id where requester_id =?", new Object[]{id}, new BeanPropertyRowMapper<>(RequestTO.class) ); @@ -41,8 +52,7 @@ public List getRequestByRequesterId(@PathVariable("id") Integer id) { @ApiOperation("Get all requests") @GetMapping("/api/v1/requests") public List getAllRequests() { - return this.jdbcTemplate.query("select select request_start_date, request_report_date, request_status.req_status from requests inner join request_status on" + - "requests.request_status_id = request_status.request_status_id", + return this.jdbcTemplate.query("select request_start_date, request_report_date, request_status.req_status from requests inner join request_status on requests.request_status_id = request_status.request_status_id", new BeanPropertyRowMapper(RequestTO.class) ); } @@ -60,5 +70,62 @@ public void approveRequest(@PathVariable("id") Integer request_id) { public void declineRequest(@PathVariable("id") Integer request_id) { this.jdbcTemplate.update("update requests set request_status_id = 2 where request_status_id = 1 and request_id = ?", request_id); } + + @CrossOrigin + @ApiOperation("validating employee with OIDC") + @PostMapping("/validate") + public Map checkToken(@RequestHeader("access-token") String token){ + Map response = new HashMap<>(); + + Verifier verifier = RSAVerifier.newVerifier(Paths.get("public_key.pem")); + try { + // Verify and decode the encoded string JWT to a rich object + JWT jwt = JWT.getDecoder().decode(token,verifier); + response.put("success", true); + response.put("decoded_token", jwt); + return response; + } catch (JWTException e) { + e.printStackTrace(); + response.put("success", false); + return response; + } + } + + @CrossOrigin + @ApiOperation("Checking available email") + @GetMapping("/verifymail/{email}") + public Map check_employee_exits(@PathVariable("email") String email) { + Map response = new HashMap<>(); + + response.put("response", this.jdbcTemplate.query("select * from employee where employee_email = ?", + new Object[]{email}, + BeanPropertyRowMapper.newInstance(Employee.class) + ) ); + return response; + } + + + @CrossOrigin + @ApiOperation("Checking available email") + @PostMapping("/addemployee") + public Map addemployeeDetails(@RequestBody Employee employeeDetails) { + Map response = new HashMap<>(); + + SimpleJdbcInsert insertActor = new SimpleJdbcInsert(jdbcTemplate).withTableName("employee").usingGeneratedKeyColumns("employee_id"); + Map insertEmployeeDetails = new HashMap<>(); + insertEmployeeDetails.put("employee_firstname", employeeDetails.getEmployee_firstname()); + insertEmployeeDetails.put("employee_lastname", employeeDetails.getEmployee_lastname()); + insertEmployeeDetails.put("employee_email", employeeDetails.getEmployee_email()); + + Number Key = insertActor.executeAndReturnKey(insertEmployeeDetails); + if (Key != null){ + response.put("success",true); + response.put("employee_id",Key.longValue()); + }else { + response.put("success",false); + response.put("msg","Failed to add new employee, try again later"); + } + return response; + } } diff --git a/src/main/java/io/turntabl/employementprofilingsystem/EmployementProfilingSystemApplication.java b/src/main/java/io/turntabl/employementprofilingsystem/EmployementProfilingSystemApplication.java index 8a8eec0..d905396 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/EmployementProfilingSystemApplication.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/EmployementProfilingSystemApplication.java @@ -17,6 +17,7 @@ public class EmployementProfilingSystemApplication { public static void main(String[] args) throws GeneralSecurityException, IOException { SpringApplication.run(EmployementProfilingSystemApplication.class, args); + } } diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java index 11bb712..b695986 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java @@ -10,11 +10,12 @@ public class RequestTO { private int request_status_id; private String from; private String requester_name; + private String req_status; public RequestTO() { } - public RequestTO(int request_id, int requester_id, Date request_start_date, Date request_report_date, int request_status_id, String from, String requester_name) { + public RequestTO(int request_id, int requester_id, Date request_start_date, Date request_report_date, int request_status_id, String from, String requester_name, String req_status) { this.request_id = request_id; this.requester_id = requester_id; this.request_start_date = request_start_date; @@ -22,6 +23,7 @@ public RequestTO(int request_id, int requester_id, Date request_start_date, Date this.request_status_id = request_status_id; this.from = from; this.requester_name = requester_name; + this.req_status = req_status; } public int getRequest_id() { @@ -80,6 +82,14 @@ public void setRequester_name(String requester_name) { this.requester_name = requester_name; } + public String getReq_status() { + return req_status; + } + + public void setReq_status(String req_status) { + this.req_status = req_status; + } + @Override public String toString() { return "RequestTO{" + @@ -90,6 +100,7 @@ public String toString() { ", request_status_id=" + request_status_id + ", from='" + from + '\'' + ", requester_name='" + requester_name + '\'' + + ", req_status='" + req_status + '\'' + '}'; } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2812454..616a8fc 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,8 @@ spring.datasource.driverClassName = org.postgresql.Driver -spring.datasource.url = $(SPRING_DATASOURCE_URL) -spring.datasource.username = $(SPRING_DATASOURCE_USERNAME) -spring.datasource.password = $(SPRING_DATASOURCE_PASSWORD) - +#spring.datasource.url = $(SPRING_DATASOURCE_URL) +#spring.datasource.username = $(SPRING_DATASOURCE_USERNAME) +#spring.datasource.password = $(SPRING_DATASOURCE_PASSWORD) +spring.datasource.url = jdbc:postgresql://localhost/holiday +spring.datasource.username = agyen +spring.datasource.password = 0000 From 97e1e35600abf1866190f928b137a8b0b93a5ab3 Mon Sep 17 00:00:00 2001 From: agyen Date: Wed, 4 Mar 2020 12:16:55 +0000 Subject: [PATCH 18/18] gmail service for gsuite domain --- db/holiday.sql | 6 +++--- .../Controllers/RequestController.java | 3 ++- .../Gmail/GmailService.java | 12 +++++------- .../employementprofilingsystem/Models/RequestTO.java | 7 +++++++ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/db/holiday.sql b/db/holiday.sql index 6a10ecf..35a3e92 100644 --- a/db/holiday.sql +++ b/db/holiday.sql @@ -2,10 +2,10 @@ DROP TABLE IF EXISTS employee cascade; CREATE TABLE employee ( employee_id serial primary key, - employee_firstName text not null, - employee_lastName text not null, + employee_firstName text , + employee_lastName text , employee_phoneNumber text, - employee_email text not null, + employee_email text, employee_address text, employee_role text, employee_dev_level text, diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java index c096211..b755311 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Controllers/RequestController.java @@ -70,6 +70,7 @@ public List getRequestByRequesterId(@PathVariable("id") Integer id) { public List getAllRequests() { return this.jdbcTemplate.query("select request_start_date, request_report_date, request_status.req_status from requests inner join request_status on requests.request_status_id = request_status.request_status_id", new BeanPropertyRowMapper(RequestTO.class) + ); } @@ -101,7 +102,7 @@ public Map checkToken(@RequestHeader("access-token") String toke response.put("decoded_token", jwt); return response; } catch (JWTException e) { - e.printStackTrace(); + e.getCause(); response.put("success", false); return response; } diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java index defedb1..b877c99 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Gmail/GmailService.java @@ -30,14 +30,13 @@ public class GmailService { private static final String CREDENTIALS_FILE_PATH = "credentials.json"; public static void sendMail(String from, String to, String subject, String bodyText) throws IOException, GeneralSecurityException { - Gmail service = getGmail(); + Gmail service = getGmail(from); System.out.println(service.getApplicationName()); System.out.println(service.getBaseUrl()); System.out.println(service.users().toString()); - String user = from; try { - service.users().messages().send(user, + service.users().messages().send(from, createMessageWithEmail(createEmail(to, from, subject, bodyText)) ).execute(); } catch (MessagingException e) { @@ -45,7 +44,7 @@ public static void sendMail(String from, String to, String subject, String bodyT } } - public static Gmail getGmail() throws GeneralSecurityException, IOException { + public static Gmail getGmail(String from) throws GeneralSecurityException, IOException { HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential gcFromJson = GoogleCredential @@ -56,13 +55,12 @@ public static Gmail getGmail() throws GeneralSecurityException, IOException { .setTransport(gcFromJson.getTransport()) .setJsonFactory(gcFromJson.getJsonFactory()) .setServiceAccountId(gcFromJson.getServiceAccountId()) - .setServiceAccountUser( "dawud.ismail@turntabl.io") + .setServiceAccountUser( from) .setServiceAccountPrivateKey(gcFromJson.getServiceAccountPrivateKey()) .setServiceAccountScopes(gcFromJson.getServiceAccountScopes()) .setTokenServerEncodedUrl(gcFromJson.getTokenServerEncodedUrl()) .build(); - boolean token = credential.refreshToken(); - System.out.println(token); + return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME) .build(); diff --git a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java index b695986..3fdf413 100644 --- a/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java +++ b/src/main/java/io/turntabl/employementprofilingsystem/Models/RequestTO.java @@ -1,5 +1,6 @@ package io.turntabl.employementprofilingsystem.Models; +import java.text.SimpleDateFormat; import java.util.Date; public class RequestTO { @@ -26,6 +27,10 @@ public RequestTO(int request_id, int requester_id, Date request_start_date, Date this.req_status = req_status; } +// SimpleDateFormat DateFor = new SimpleDateFormat("E, dd MMMM yyyy"); +// String startDate = DateFor.format(request.getRequest_start_date()); +// String reportDate = DateFor.format(request.getRequest_report_date()); + public int getRequest_id() { return request_id; } @@ -43,6 +48,8 @@ public void setRequester_id(int requester_id) { } public Date getRequest_start_date() { + // SimpleDateFormat DateFor = new SimpleDateFormat("E, dd MMMM yyyy"); + // String request_start_date = DateFor.format(getRequest_start_date()); return request_start_date; }