Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions Week 01/Lecture 02/task7/src/main/java/model/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@ public Employee(String id, String name, LocalDate dateOfBirth, String address, S
this.department = department;
}

/**
* Parses an array of attributes into an Employee object.
*
* @param attributes an array of strings representing the employee's id, name, date of birth, address, and department.
* @return an Employee object created from the provided attributes.
*/
public static Employee fromCSV(String[] attributes) {
String id = attributes[0];
String name = attributes[1];
LocalDate dateOfBirth = DateUtils.parseDate(attributes[2]);
String address = attributes[3];
String department = attributes[4];
return new Employee(id, name, dateOfBirth, address, department);
}

/**
* Converts the Employee object to a CSV string format.
*
Expand Down
14 changes: 10 additions & 4 deletions Week 01/Lecture 02/task7/src/main/java/utils/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class DateUtils {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");
private static final DateTimeFormatter DATE_FORMATER = DateTimeFormatter.ofPattern("d/M/yyyy");

/**
* Parses a date string in the format "d/M/yyyy" and returns a LocalDate object.
Expand All @@ -13,8 +14,13 @@ public class DateUtils {
* @return the parsed LocalDate object
* @throws IllegalArgumentException if the date string cannot be parsed
*/
public static LocalDate parseDate(String dateString) {
return LocalDate.parse(dateString, formatter);
public static LocalDate parseDate(String dateStr) {
try {
return LocalDate.parse(dateStr, DATE_FORMATER);
} catch (DateTimeParseException e) {
System.out.println("Error parsing date: " + dateStr);
throw e;
}
}

/**
Expand All @@ -24,6 +30,6 @@ public static LocalDate parseDate(String dateString) {
* @return the formatted string in the specified format
*/
public static String formatDate(LocalDate date) {
return date.format(formatter);
return date.format(DATE_FORMATER);
}
}
19 changes: 17 additions & 2 deletions Week 01/Lecture 02/task7/src/main/java/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static List<Employee> readEmployeesFromCSVManual(String filePath) throws
br.readLine(); // Skip header
while ((line = br.readLine()) != null) {
String[] attributes = line.split(",");
Employee employee = Employee.fromCSV(attributes);
Employee employee = fromCSV(attributes);
employees.add(employee);
}
} catch (FileNotFoundException e) {
Expand All @@ -55,7 +55,7 @@ public static List<Employee> readEmployeesFromCSVOpenCSV(String filePath) throws
String[] line;
reader.readNext(); // Skip header
while ((line = reader.readNext()) != null) {
Employee employee = Employee.fromCSV(line);
Employee employee = fromCSV(line);
employees.add(employee);
}
} catch (FileNotFoundException e) {
Expand Down Expand Up @@ -83,4 +83,19 @@ public static void writeEmployeesToCSV(List<Employee> employees, String filePath
throw new FileNotFoundException("Error writing employee" + e);
}
}

/**
* Parses an array of attributes into an Employee object.
*
* @param attributes an array of strings representing the employee's id, name, date of birth, address, and department.
* @return an Employee object created from the provided attributes.
*/
public static Employee fromCSV(String[] attributes) {
String id = attributes[0];
String name = attributes[1];
LocalDate dateOfBirth = DateUtils.parseDate(attributes[2]);
String address = attributes[3];
String department = attributes[4];
return new Employee(id, name, dateOfBirth, address, department);
}
}
130 changes: 130 additions & 0 deletions Week 03/Lecture 05/Assignment 01/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# 👨🏻‍🏫 Lecture 05 - Basic Backend, Spring
> This repository is created as a part of assignment for Lecture 05 - Basic Backend, Spring

## 🔎 Assignment 01 - Practice the Example
### 🌳 Project Structure
```bash
lecture_5
├── .mvn/wrapper/
│ └── maven-wrapper.properties
├── src/main/
│ ├── java/com/example/lecture_5_hw/
│ │ ├── controller/
│ │ │ └── ContactController.java
│ │ ├── model/
│ │ │ └── Contact.java
│ │ ├── repository/
│ │ │ └── ContactRepository.java
│ │ └── Lecture5Application.java
│ └── resources/
│ └── application.properties
├── .gitignore
├── mvnw
├── mvnw.cmd
├── pom.xml
├── run.bat
└── run.sh
```

### 🧩 SQL Query Data
Here is the SQL query to create the database, table, and instantiate some data, given by the [reference repository](https://github.com/NguyenVanTrieu/spring-crud).
```sql
-- Create the database
CREATE DATABASE week3_lecture5;

-- Use the database
USE week3_lecture5;

-- Create the contact table
CREATE TABLE `contact` (
`name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`age` int(3) NOT NULL,
`dob` date NOT NULL,
`email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`id` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


ALTER TABLE `contact`
ADD PRIMARY KEY (`id`);

-- Insert dummy data into the contact table
INSERT INTO `contact` (`name`, `age`, `dob`, `email`, `id`) VALUES
('Daniel Jon', 22, '2017-11-15', 'jon@gamil.com', "1d61dd2d-f084-45e5-b561-c50dfed5468b"),
('David Jame', 22, '2017-11-01', 'jame@gmail.com', "d5353061-f51e-4871-9623-37abe50dd14c"),
('Hela', 23, '1997-11-01', 'hela@gmail.com', "884422c6-3839-4052-b1f5-a6a349248a58"),
('Bruno', 25, '1990-11-01', 'bruno@gmail.com', "5d36942d-94e6-4f70-9106-8bd88c445585");
```

and here is the query to drop the database
```sql
-- Drop the database
DROP DATABASE IF EXISTS week3_lecture5;
```

### ⚙️ How to run the program
1. Go to the `lecture_5` directory by using this command
```bash
$ cd lecture_5
```
2. Make sure you have maven installed on your computer, use `mvn -v` to check the version.
3. If you are using windows, you can run the program by using this command.
```bash
$ ./run.bat
```
And if you are using Linux, you can run the program by using this command.
```bash
$ chmod +x run.sh
$ ./run.sh
```

If all the instruction is well executed, the main-view will be something like this.

![Screenshot](img/start.png)

### 📸 Screenshots
Here is some result of the APIs created. based on the postman collection with slight modification.
<br>
#### Initial state

![Screenshot](img/init.png)

1. **Get All Contacts**
`(GET /api/v1/contact)`

![Screenshot](img/api1.png)
2. **Get Contact By ID**
`(GET /api/v1/contact/b002747b-4cc3-4a81-bd7e-e3184da0410a)`

![Screenshot](img/api2.png)
3. **Add New Contact**
`(POST /api/v1/contact)`

Body (Raw):
```json
{
"name": "Trieu",
"dob": "2017-11-14T17:00:00.000+00:00",
"age": 22,
"email": "trieu@gamil.com",
"id": "b002747b-4cc3-4a81-bd7e-e3184da0410p"
}
```

![Screenshot](img/api3.png)
![Screenshot](img/api32.png)
4. **Edit Contact**
`(PUT /api/v1/contact/b002747b-4cc3-4a81-bd7e-e3184da0410p)`

Body (Raw):
```json
{
"name": "Tom",
"dob": "2017-11-14T17:00:00.000+00:00",
"age": 23,
"email": "jon@gmail.com"
}
```

![Screenshot](img/api4.png)
![Screenshot](img/api42.png)
Binary file added Week 03/Lecture 05/Assignment 01/img/api1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 03/Lecture 05/Assignment 01/img/api2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 03/Lecture 05/Assignment 01/img/api3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 03/Lecture 05/Assignment 01/img/api32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 03/Lecture 05/Assignment 01/img/api4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 03/Lecture 05/Assignment 01/img/api42.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 03/Lecture 05/Assignment 01/img/init.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 03/Lecture 05/Assignment 01/img/start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Week 03/Lecture 05/Assignment 01/lecture_5/run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
echo Building the project with Maven...
mvn clean install && java -jar target/lecture_5-1.0-SNAPSHOT.jar
3 changes: 3 additions & 0 deletions Week 03/Lecture 05/Assignment 01/lecture_5/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
echo Building the project with Maven...
mvn clean install && java -jar target/lecture_5-1.0-SNAPSHOT.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"info": {
"_postman_id": "859f23f0-952d-4e0d-b176-9816219a20e5",
"name": "Lecture 05 - Assignment 02",
"description": "This postman collection is created by Michael Leon as a part of assignment 02 for Lecture 05 - Basic Backend, Spring",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json",
"_exporter_id": "34693283"
},
"item": [
{
"name": "Get All Employees",
"request": {
"method": "GET",
"header": [],
"url": "localhost:8080/api/v1/employee"
},
"response": []
},
{
"name": "Get Employee By ID",
"request": {
"method": "GET",
"header": [],
"url": "localhost:8080/api/v1/employee/3ccd3c90-890e-41c4-9fa3-456f3d97f999"
},
"response": []
},
{
"name": "Add New Employee",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"id\": \"b002747b-4cc3-4a81-bd7e-e3184da0410a\",\r\n \"name\": \"Michael Leon\",\r\n \"dob\": \"2003-12-18\",\r\n \"address\": \"Anytime anywhere\",\r\n \"department\": \"MOBILE\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "localhost:8080/api/v1/employee"
},
"response": []
},
{
"name": "Edit Employee",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"name\": \"Leon Michael\",\r\n \"dob\": \"2003-12-18\",\r\n \"address\": \"Anytime anywhere anyplace\",\r\n \"department\": \"MOBILE\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "localhost:8080/api/v1/employee/b002747b-4cc3-4a81-bd7e-e3184da0410a"
},
"response": []
},
{
"name": "Delete Employee",
"request": {
"method": "DELETE",
"header": [],
"url": "localhost:8080/api/v1/employee/b002747b-4cc3-4a81-bd7e-e3184da0410a"
},
"response": []
},
{
"name": "Post CSV",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "file",
"type": "file",
"src": "/C:/Users/user/Documents/College/ITB - Informatika/Semester 7/Kerja Praktik (KP)/java-leon/Week 03/Lecture 05/Assignment 02/lecture_5_hw/src/main/java/com/example/lecture_5_hw/data/ImportData.csv"
}
]
},
"url": "localhost:8080/api/v1/employee/upload-csv"
},
"response": []
},
{
"name": "Get Employees by Department",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:8080/api/v1/employee?department=QA",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"v1",
"employee"
],
"query": [
{
"key": "department",
"value": "QA"
}
]
}
},
"response": []
}
]
}
Loading