Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@SpringBootApplication
public class TicketTrackingSystemApplication {

public static void main(String[] args) {
static void main(String[] args) {
SpringApplication.run(TicketTrackingSystemApplication.class, args);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.hackyourfuture.tickettrackingsystem.controller;

import lombok.RequiredArgsConstructor;
import net.hackyourfuture.tickettrackingsystem.dto.response.ProjectSummaryResponse;
import net.hackyourfuture.tickettrackingsystem.service.ProjectService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/projects")
public class ProjectController {

private final ProjectService projectService;

@GetMapping
public List<ProjectSummaryResponse> getAllProjects() {
return projectService.getAllProjects();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.hackyourfuture.tickettrackingsystem.dto.response;

public record ProjectSummaryResponse(
Long id,
String name,
int openTickets,
int inProgressTickets,
int closedTickets
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.hackyourfuture.tickettrackingsystem.repository;

import lombok.RequiredArgsConstructor;
import net.hackyourfuture.tickettrackingsystem.dto.response.ProjectSummaryResponse;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class ProjectRepository {
private final JdbcTemplate jdbcTemplate;

public List<ProjectSummaryResponse> findAllProjectSummaries(){
String sql = """
SELECT
p.id,
p.name,
COUNT(*) FILTER (WHERE t.status = 'OPEN') AS open_tickets,
COUNT(*) FILTER (WHERE t.status = 'IN_PROGRESS') AS in_progress_tickets,
COUNT(*) FILTER (WHERE t.status = 'CLOSED') AS closed_tickets
FROM projects p
LEFT JOIN tickets t ON p.id = t.project_id
GROUP BY p.id, p.name
ORDER BY p.name
""";
return jdbcTemplate.query(sql, projectSummaryResponseRowMapper);
}

private final RowMapper<ProjectSummaryResponse> projectSummaryResponseRowMapper =
(rs, rowNum) -> new ProjectSummaryResponse(
rs.getLong("id"),
rs.getString("name"),
rs.getInt("open_tickets"),
rs.getInt("in_progress_tickets"),
rs.getInt("closed_tickets")
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.hackyourfuture.tickettrackingsystem.service;

import lombok.RequiredArgsConstructor;
import net.hackyourfuture.tickettrackingsystem.dto.response.ProjectSummaryResponse;
import net.hackyourfuture.tickettrackingsystem.repository.ProjectRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ProjectService {

private final ProjectRepository projectRepository;

public List<ProjectSummaryResponse> getAllProjects() {
return projectRepository.findAllProjectSummaries();
}
}
10 changes: 5 additions & 5 deletions src/main/resources/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CREATE TABLE users (
);
CREATE TABLE projects (
id bigserial primary key ,
name varchar(255) not null,CHECK (char_length(name) >= 3)
name varchar(255) not null CHECK (char_length(name) >= 3)
);
CREATE TABLE tickets (
id bigserial primary key ,
Expand All @@ -21,7 +21,7 @@ CREATE TABLE tickets (
update_date TIMESTAMP,
CONSTRAINT fk_ticket_project
FOREIGN KEY (project_id)
REFERENCES project(id)
REFERENCES projects(id)

);

Expand All @@ -37,15 +37,15 @@ CREATE TABLE user_ticket (
ON DELETE CASCADE,
CONSTRAINT fk_user_ticket_ticket
FOREIGN KEY (ticket_id)
REFERENCES ticket(id)
REFERENCES tickets(id)
ON DELETE CASCADE
);

CREATE INDEX idx_tickets_status
ON ticket(status);
ON tickets(status);

CREATE INDEX idx_tickets_project_id
ON ticket(project_id);
ON tickets(project_id);

CREATE INDEX idx_user_ticket_user_id
ON user_ticket(user_id);
Loading