From e22d2b6fcac88e842d8974fc4e8dabc4a83e9818 Mon Sep 17 00:00:00 2001 From: Murtadha Date: Fri, 13 Dec 2024 17:52:33 -0500 Subject: [PATCH] v0.2.0 Switch to mongoDB --- pom.xml | 29 +--- .../controller/EducationController.java | 83 +++--------- .../io/titan/portfolio/model/Education.java | 15 ++- .../repository/EducationRepository.java | 125 ++---------------- .../portfolio/service/EducationService.java | 75 ++--------- src/main/resources/application.properties | 11 +- src/main/resources/education_schema.sql | 14 -- titan.db | Bin 12288 -> 0 bytes version.txt | 2 +- 9 files changed, 62 insertions(+), 292 deletions(-) delete mode 100644 src/main/resources/education_schema.sql delete mode 100644 titan.db diff --git a/pom.xml b/pom.xml index b0adee1..0ad1c1c 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ - 17 + 21 @@ -67,35 +67,10 @@ 3.5.6-Final - - - - - - com.github.gwenn - sqlite-dialect - 0.1.4 - - - - - org.xerial - sqlite-jdbc - 3.45.1.0 - - - org.springframework.boot - spring-boot-starter-jdbc - 3.3.4 + spring-boot-starter-data-mongodb - - diff --git a/src/main/java/io/titan/portfolio/controller/EducationController.java b/src/main/java/io/titan/portfolio/controller/EducationController.java index 87b939f..3d11c7a 100644 --- a/src/main/java/io/titan/portfolio/controller/EducationController.java +++ b/src/main/java/io/titan/portfolio/controller/EducationController.java @@ -1,18 +1,16 @@ package io.titan.portfolio.controller; -import org.springframework.dao.DataAccessException; -import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.ExceptionHandler; -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 org.springframework.web.bind.annotation.*; +// 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; @@ -29,74 +27,35 @@ public class EducationController { } @GetMapping - public ResponseEntity> getAllEducation() { - List educations = educationService.getAllEducation(); - return ResponseEntity.ok(educations); + List getAllEducation(){ + return educationService.getAllEducation(); } @GetMapping("/{id}") - public ResponseEntity getEducationById(@PathVariable Long id) { + ResponseEntity getEducationById(@PathVariable String id) { return educationService.getEducationById(id) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } @PostMapping - public ResponseEntity createEducation(@RequestBody Education education) { - try { - Education createdEducation = educationService.createEducation(education); - return ResponseEntity.status(HttpStatus.CREATED).body(createdEducation); - } catch (IllegalArgumentException e) { - return ResponseEntity.badRequest().body(null); - } + ResponseEntity createEducation(@RequestBody Education education) { + educationService.createEducation(education); + return ResponseEntity.status(HttpStatus.CREATED).build(); } @PutMapping("/{id}") - public ResponseEntity updateEducation(@PathVariable Long id, @RequestBody Education education) { + ResponseEntity updateEducation(@PathVariable String id, @RequestBody Education education) { if (!id.equals(education.id())) { return ResponseEntity.badRequest().build(); } - try { - Education updatedEducation = educationService.updateEducation(education); - return ResponseEntity.ok(updatedEducation); - } catch (EmptyResultDataAccessException e) { - return ResponseEntity.notFound().build(); - } catch (IllegalArgumentException e) { - return ResponseEntity.badRequest().body(null); - } + educationService.updateEducation(education); + return ResponseEntity.ok(education); } @DeleteMapping("/{id}") - public ResponseEntity deleteEducation(@PathVariable Long id) { - try { - educationService.deleteEducation(id); - return ResponseEntity.noContent().build(); - } catch (EmptyResultDataAccessException e) { - return ResponseEntity.notFound().build(); - } - } - - @ExceptionHandler(EmptyResultDataAccessException.class) - public ResponseEntity handleNotFound(EmptyResultDataAccessException e) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); - } - - @ExceptionHandler(IllegalArgumentException.class) - public ResponseEntity handleBadRequest(IllegalArgumentException e) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); - } - - @ExceptionHandler(DataAccessException.class) - public ResponseEntity handleDataAccessException(DataAccessException e) { - // Log the exception - e.printStackTrace(); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An unexpected error occurred"); - } - - @ExceptionHandler(Exception.class) - public ResponseEntity handleGeneralException(Exception e) { - // Log the exception - e.printStackTrace(); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An unexpected error occurred"); + ResponseEntity deleteEducation(@PathVariable String id) { + boolean deleted = educationService.deleteEducation(id); + return deleted ? ResponseEntity.ok().build() : ResponseEntity.notFound().build(); } } diff --git a/src/main/java/io/titan/portfolio/model/Education.java b/src/main/java/io/titan/portfolio/model/Education.java index d479f03..66748b2 100644 --- a/src/main/java/io/titan/portfolio/model/Education.java +++ b/src/main/java/io/titan/portfolio/model/Education.java @@ -1,14 +1,19 @@ package io.titan.portfolio.model; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + import java.util.List; +@Document(collection = "education") public record Education( - Long id, + @Id String id, String title, String organization, String logo, String location, - String duration, + Integer fromDate, + Integer toDate, String description, List achievements, List skills, @@ -17,13 +22,13 @@ public record Education( String techStackTitle ) { public Education() { - this(null, null, null, null, null, null, null, null, null, null, null, null); + this(null, null, null, null, null, null, null, null, null, null, null, null, null); } - public Education(String title, String organization, String logo, String location, String duration, + public Education(String title, String organization, String logo, String location, Integer fromDate, Integer toDate, String description, List achievements, List skills, List techStack, String skillsTitle, String techStackTitle) { - this(null, title, organization, logo, location, duration, description, + this(null, title, organization, logo, location, fromDate, toDate, description, achievements, skills, techStack, skillsTitle, techStackTitle); } } diff --git a/src/main/java/io/titan/portfolio/repository/EducationRepository.java b/src/main/java/io/titan/portfolio/repository/EducationRepository.java index 39c315c..8ab7452 100644 --- a/src/main/java/io/titan/portfolio/repository/EducationRepository.java +++ b/src/main/java/io/titan/portfolio/repository/EducationRepository.java @@ -1,119 +1,22 @@ 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; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; +// 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 interface EducationRepository extends MongoRepository{ - public EducationRepository(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; - } - - private static final RowMapper 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 findAll() { - String sql = "SELECT * FROM Education"; - return jdbcTemplate.query(sql, EDUCATION_ROW_MAPPER); - } - - public Optional findById(Long id) { - String sql = "SELECT * FROM Education WHERE id = ?"; - List 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); - } } diff --git a/src/main/java/io/titan/portfolio/service/EducationService.java b/src/main/java/io/titan/portfolio/service/EducationService.java index 4386c28..6fac2a6 100644 --- a/src/main/java/io/titan/portfolio/service/EducationService.java +++ b/src/main/java/io/titan/portfolio/service/EducationService.java @@ -2,10 +2,6 @@ package io.titan.portfolio.service; import io.titan.portfolio.model.Education; import io.titan.portfolio.repository.EducationRepository; - -import org.springframework.dao.DataAccessException; -import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.stereotype.Service; import java.util.List; @@ -19,76 +15,27 @@ public class EducationService{ this.educationRepository = educationRepository; } - public List getAllEducation() { - try { - return educationRepository.findAll(); - } catch (DataAccessException e) { - // Log the error - System.err.println("Error retrieving all education records: " + e.getMessage()); - throw new RuntimeException("Failed to retrieve education records", e); - } + public List getAllEducation(){ + return educationRepository.findAll(); } - public Optional getEducationById(Long id) { - try { - return educationRepository.findById(id); - } catch (DataAccessException e) { - // Log the error - System.err.println("Error retrieving education by id: " + e.getMessage()); - throw new RuntimeException("Failed to retrieve education record", e); - } + public Optional getEducationById(String id){ + return educationRepository.findById(id); } public Education createEducation(Education education) { - try { - return educationRepository.save(education); - } catch (DataIntegrityViolationException e) { - // Log the error - System.err.println("Error creating education record: " + e.getMessage()); - throw new IllegalArgumentException("Invalid education data: " + e.getMessage()); - } catch (DataAccessException e) { - // Log the error - System.err.println("Error creating education record: " + e.getMessage()); - throw new RuntimeException("Failed to create education record", e); - } + return educationRepository.save(education); } - public Education updateEducation(Education education) { - try { - if (!educationRepository.existsById(education.id())) { - throw new EmptyResultDataAccessException("Education record not found with id: " + education.id(), 1); - } - educationRepository.update(education); - return education; - } catch (EmptyResultDataAccessException e) { - // Log the error - System.err.println("Education record not found: " + e.getMessage()); - throw e; - } catch (DataIntegrityViolationException e) { - // Log the error - System.err.println("Error updating education record: " + e.getMessage()); - throw new IllegalArgumentException("Invalid education data: " + e.getMessage()); - } catch (DataAccessException e) { - // Log the error - System.err.println("Error updating education record: " + e.getMessage()); - throw new RuntimeException("Failed to update education record", e); - } + public void updateEducation(Education education) { + educationRepository.save(education); } - public void deleteEducation(Long id) { - try { - if (!educationRepository.existsById(id)) { - throw new EmptyResultDataAccessException("Education record not found with id: " + id, 1); - } + public boolean deleteEducation(String id) { + if (educationRepository.existsById(id)) { educationRepository.deleteById(id); - } catch (EmptyResultDataAccessException e) { - // Log the error - System.err.println("Education record not found: " + e.getMessage()); - throw e; - } catch (DataAccessException e) { - // Log the error - System.err.println("Error deleting education record: " + e.getMessage()); - throw new RuntimeException("Failed to delete education record", e); + return true; } + return false; } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c45cc49..1672902 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,11 +1,6 @@ -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.data.mongodb.uri=mongodb://portfolio_user:portfolio_P%40ssword@localhost:27017/portfolio_db +spring.application.name=titan_backend spring.devtools.restart.enabled=true server.port=8282 -# logging.level.root=DEBUG \ No newline at end of file +# logging.level.root=DEBUG diff --git a/src/main/resources/education_schema.sql b/src/main/resources/education_schema.sql deleted file mode 100644 index e73a5ea..0000000 --- a/src/main/resources/education_schema.sql +++ /dev/null @@ -1,14 +0,0 @@ -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 -); \ No newline at end of file diff --git a/titan.db b/titan.db deleted file mode 100644 index 46102940300f5c59d6a5ca0f99e0b96d7f0e86a4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeHN-EQ1O6!tbPErOQj0#&{0i4bb5dN(BnA&?MkH>O=Re`Gg=URAU9S?`eXj5{+< zy5%ZSDdJUlB3^?x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