1 | package com.example.skychasemk.services;
|
---|
2 |
|
---|
3 | import com.example.skychasemk.dto.SupportTicketDTO;
|
---|
4 | import com.example.skychasemk.model.SupportTicket;
|
---|
5 | import com.example.skychasemk.repository.SupportTicketRepository;
|
---|
6 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
7 | import org.springframework.stereotype.Service;
|
---|
8 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
9 | import org.springframework.web.bind.annotation.RequestBody;
|
---|
10 |
|
---|
11 | import java.time.LocalDate;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Optional;
|
---|
14 |
|
---|
15 | import static com.example.skychasemk.model.SupportTicket.TicketStatus.OPEN;
|
---|
16 |
|
---|
17 | @Service
|
---|
18 | public class SupportTicketService {
|
---|
19 |
|
---|
20 | @Autowired
|
---|
21 | private SupportTicketRepository supportTicketRepository;
|
---|
22 |
|
---|
23 |
|
---|
24 | public SupportTicketService(SupportTicketRepository repository){
|
---|
25 | this.supportTicketRepository=repository;
|
---|
26 | }
|
---|
27 | public List<SupportTicket> getAllTickets() {
|
---|
28 | return supportTicketRepository.findTickets();
|
---|
29 | }
|
---|
30 | public List<SupportTicket> getResolvedTickets() {
|
---|
31 | return supportTicketRepository.findResolvedTickets();
|
---|
32 | }
|
---|
33 |
|
---|
34 | public Optional<SupportTicket> getTicketById(Integer ticketID) {
|
---|
35 | return supportTicketRepository.findById(ticketID);
|
---|
36 | }
|
---|
37 |
|
---|
38 | @PostMapping
|
---|
39 | public SupportTicket createTicket(@RequestBody SupportTicketDTO dto) {
|
---|
40 | SupportTicket ticket = new SupportTicket();
|
---|
41 | ticket.setUserId(dto.getUserId());
|
---|
42 | ticket.setDateCreated(LocalDate.now());
|
---|
43 | ticket.setStatus(OPEN);
|
---|
44 | ticket.setDescription(dto.getDescription());
|
---|
45 | return supportTicketRepository.save(ticket);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public SupportTicket save(SupportTicket ticket) {
|
---|
49 | return supportTicketRepository.save(ticket);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public SupportTicket updateTicket(Integer id, SupportTicket.TicketStatus newStatus) {
|
---|
53 | return supportTicketRepository.findById(id)
|
---|
54 | .map(ticket -> {
|
---|
55 | ticket.setStatus(newStatus);
|
---|
56 | if (newStatus == SupportTicket.TicketStatus.RESOLVED) {
|
---|
57 | ticket.setDateResolved(LocalDate.now());
|
---|
58 | }
|
---|
59 | return supportTicketRepository.save(ticket);
|
---|
60 | })
|
---|
61 | .orElseThrow(() -> new RuntimeException("Ticket not found"));
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|