v0.1.0 Implement Education data in portfolio package
This commit is contained in:
parent
1b0e5400c9
commit
989528fce5
27 changed files with 862 additions and 313 deletions
19
src/main/java/io/titan/Application.java
Normal file
19
src/main/java/io/titan/Application.java
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package io.titan;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(Application.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
|
||||
// log.info("Something new!");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
package io.titan.portfolio.controller;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import io.titan.portfolio.model.Education;
|
||||
import io.titan.portfolio.service.EducationService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/portfolio/education")
|
||||
public class EducationController {
|
||||
private final EducationService educationService;
|
||||
|
||||
EducationController(EducationService educationService){
|
||||
this.educationService = educationService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
List<Education> getAllEducation(){
|
||||
return educationService.getAllEducation();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
ResponseEntity<Education> getEducationById(@PathVariable Long id) {
|
||||
return educationService.getEducationById(id)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
ResponseEntity<Void> createEducation(@RequestBody Education education) {
|
||||
educationService.createEducation(education);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
ResponseEntity<Void> updateEducation(@PathVariable Long id, @RequestBody Education education) {
|
||||
if (!id.equals(education.id())) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
educationService.updateEducation(education);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
ResponseEntity<Void> deleteEducation(@PathVariable Long id) {
|
||||
boolean deleted = educationService.deleteEducation(id);
|
||||
if (deleted) {
|
||||
return ResponseEntity.ok().build();
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/main/java/io/titan/portfolio/model/Education.java
Normal file
29
src/main/java/io/titan/portfolio/model/Education.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package io.titan.portfolio.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record Education(
|
||||
Long id,
|
||||
String title,
|
||||
String organization,
|
||||
String logo,
|
||||
String location,
|
||||
String duration,
|
||||
String description,
|
||||
List<String> achievements,
|
||||
List<String> skills,
|
||||
List<String> techStack,
|
||||
String skillsTitle,
|
||||
String techStackTitle
|
||||
) {
|
||||
public Education() {
|
||||
this(null, null, null, null, null, null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
public Education(String title, String organization, String logo, String location, String duration,
|
||||
String description, List<String> achievements, List<String> skills,
|
||||
List<String> techStack, String skillsTitle, String techStackTitle) {
|
||||
this(null, title, organization, logo, location, duration, description,
|
||||
achievements, skills, techStack, skillsTitle, techStackTitle);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
package io.titan.portfolio.repository;
|
||||
|
||||
import io.titan.portfolio.model.Education;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||
import org.springframework.jdbc.support.KeyHolder;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.Statement;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public class EducationRepository {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public EducationRepository(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
private static final RowMapper<Education> EDUCATION_ROW_MAPPER = (rs, rowNum) -> new Education(
|
||||
rs.getLong("id"),
|
||||
rs.getString("title"),
|
||||
rs.getString("organization"),
|
||||
rs.getString("logo"),
|
||||
rs.getString("location"),
|
||||
rs.getString("duration"),
|
||||
rs.getString("description"),
|
||||
Arrays.asList(rs.getString("achievements").split(",")),
|
||||
Arrays.asList(rs.getString("skills").split(",")),
|
||||
Arrays.asList(rs.getString("tech_stack").split(",")),
|
||||
rs.getString("skills_title"),
|
||||
rs.getString("tech_stack_title")
|
||||
);
|
||||
|
||||
public List<Education> findAll() {
|
||||
String sql = "SELECT * FROM Education";
|
||||
return jdbcTemplate.query(sql, EDUCATION_ROW_MAPPER);
|
||||
}
|
||||
|
||||
public Optional<Education> findById(Long id) {
|
||||
String sql = "SELECT * FROM Education WHERE id = ?";
|
||||
List<Education> results = jdbcTemplate.query(sql, EDUCATION_ROW_MAPPER, id);
|
||||
return results.isEmpty() ? Optional.empty() : Optional.of(results.get(0));
|
||||
}
|
||||
|
||||
public Education save(Education education) {
|
||||
String sql = "INSERT INTO Education (title, organization, logo, location, duration, description, " +
|
||||
"achievements, skills, tech_stack, skills_title, tech_stack_title) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
KeyHolder keyHolder = new GeneratedKeyHolder();
|
||||
jdbcTemplate.update(connection -> {
|
||||
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
ps.setString(1, education.title());
|
||||
ps.setString(2, education.organization());
|
||||
ps.setString(3, education.logo());
|
||||
ps.setString(4, education.location());
|
||||
ps.setString(5, education.duration());
|
||||
ps.setString(6, education.description());
|
||||
ps.setString(7, String.join(",", education.achievements()));
|
||||
ps.setString(8, String.join(",", education.skills()));
|
||||
ps.setString(9, String.join(",", education.techStack()));
|
||||
ps.setString(10, education.skillsTitle());
|
||||
ps.setString(11, education.techStackTitle());
|
||||
return ps;
|
||||
}, keyHolder);
|
||||
|
||||
Long generatedId = keyHolder.getKey().longValue();
|
||||
return new Education(
|
||||
generatedId,
|
||||
education.title(),
|
||||
education.organization(),
|
||||
education.logo(),
|
||||
education.location(),
|
||||
education.duration(),
|
||||
education.description(),
|
||||
education.achievements(),
|
||||
education.skills(),
|
||||
education.techStack(),
|
||||
education.skillsTitle(),
|
||||
education.techStackTitle()
|
||||
);
|
||||
}
|
||||
|
||||
public void update(Education education) {
|
||||
String sql = "UPDATE Education SET title = ?, organization = ?, logo = ?, location = ?, " +
|
||||
"duration = ?, description = ?, achievements = ?, skills = ?, tech_stack = ?, " +
|
||||
"skills_title = ?, tech_stack_title = ? WHERE id = ?";
|
||||
jdbcTemplate.update(sql,
|
||||
education.title(),
|
||||
education.organization(),
|
||||
education.logo(),
|
||||
education.location(),
|
||||
education.duration(),
|
||||
education.description(),
|
||||
String.join(",", education.achievements()),
|
||||
String.join(",", education.skills()),
|
||||
String.join(",", education.techStack()),
|
||||
education.skillsTitle(),
|
||||
education.techStackTitle(),
|
||||
education.id()
|
||||
);
|
||||
}
|
||||
|
||||
public boolean existsById(Long id) {
|
||||
String sql = "SELECT COUNT(*) FROM Education WHERE id = ?";
|
||||
Integer count = jdbcTemplate.queryForObject(sql, Integer.class, id);
|
||||
return count != null && count > 0;
|
||||
}
|
||||
|
||||
public void deleteById(Long id) {
|
||||
String sql = "DELETE FROM Education WHERE id = ?";
|
||||
jdbcTemplate.update(sql, id);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package io.titan.portfolio.service;
|
||||
|
||||
import io.titan.portfolio.model.Education;
|
||||
import io.titan.portfolio.repository.EducationRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class EducationService{
|
||||
private final EducationRepository educationRepository;
|
||||
|
||||
public EducationService(EducationRepository educationRepository){
|
||||
this.educationRepository = educationRepository;
|
||||
}
|
||||
|
||||
public List<Education> getAllEducation(){
|
||||
return educationRepository.findAll();
|
||||
}
|
||||
|
||||
public Optional<Education> getEducationById(Long id){
|
||||
return educationRepository.findById(id);
|
||||
}
|
||||
|
||||
public Education createEducation(Education education) {
|
||||
return educationRepository.save(education);
|
||||
}
|
||||
|
||||
public void updateEducation(Education education) {
|
||||
educationRepository.update(education);
|
||||
}
|
||||
|
||||
public boolean deleteEducation(Long id) {
|
||||
if (educationRepository.existsById(id)) {
|
||||
educationRepository.deleteById(id);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
11
src/main/resources/application.properties
Normal file
11
src/main/resources/application.properties
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
spring.datasource.url=jdbc:sqlite:titan.db
|
||||
spring.datasource.driver-class-name=org.sqlite.JDBC
|
||||
|
||||
spring.sql.init.mode=always
|
||||
spring.sql.init.schema-locations=classpath:education_schema.sql
|
||||
|
||||
spring.application.name=demo
|
||||
spring.devtools.restart.enabled=true
|
||||
|
||||
server.port=8282
|
||||
# logging.level.root=DEBUG
|
||||
14
src/main/resources/education_schema.sql
Normal file
14
src/main/resources/education_schema.sql
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
CREATE TABLE IF NOT EXISTS Education (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
organization TEXT NOT NULL,
|
||||
logo TEXT,
|
||||
location TEXT,
|
||||
duration TEXT,
|
||||
description TEXT,
|
||||
achievements TEXT,
|
||||
skills TEXT,
|
||||
tech_stack TEXT,
|
||||
skills_title TEXT,
|
||||
tech_stack_title TEXT
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue