1+ package com .itgura .service .impl ;
2+
3+ import com .itgura .entity .Stream ;
4+ import com .itgura .exception .ValueNotExistException ;
5+ import com .itgura .repository .StreamRepository ;
6+ import com .itgura .request .dto .UserResponseDto ;
7+ import com .itgura .response .dto .StreamResponseDto ;
8+ import com .itgura .response .dto .mapper .StreamMapper ;
9+ import com .itgura .service .UserDetailService ;
10+ import com .itgura .service .UserStreamService ;
11+ import com .itgura .util .UserUtil ;
12+ import jakarta .transaction .Transactional ;
13+ import jakarta .ws .rs .ForbiddenException ;
14+ import org .springframework .beans .factory .annotation .Autowired ;
15+ import org .springframework .stereotype .Service ;
16+
17+ import java .util .List ;
18+ import java .util .Optional ;
19+ import java .util .UUID ;
20+
21+ @ Service
22+ public class StreamServiceImpl implements UserStreamService {
23+ @ Autowired
24+ private StreamRepository streamRepository ;
25+
26+ @ Autowired
27+ private UserDetailService userDetailService ;
28+
29+ @ Override
30+ @ Transactional
31+ public String createStream (String stream ) {
32+ try {
33+ UserResponseDto loggedUserDetails = userDetailService .getLoggedUserDetails (UserUtil .extractToken ());
34+ if (loggedUserDetails == null ) {
35+ throw new ValueNotExistException ("User not found" );
36+ }
37+ if (!(loggedUserDetails .getUserRoles ().equals ("ADMIN" ))) {
38+ throw new ForbiddenException ("User is not authorized to perform this operation" );
39+ } else {
40+ Stream stream1 = new Stream ();
41+ stream1 .setStream (stream );
42+ streamRepository .save (stream1 );
43+ return "Stream saved successfully" ;
44+ }
45+ } catch (Exception e ) {
46+ throw new RuntimeException (e );
47+ }
48+ }
49+
50+ @ Override
51+ @ Transactional
52+ public String deleteStream (UUID id ) {
53+ try {
54+ UserResponseDto loggedUserDetails = userDetailService .getLoggedUserDetails (UserUtil .extractToken ());
55+ if (loggedUserDetails == null ) {
56+ throw new ValueNotExistException ("User not found" );
57+ }
58+ if (!(loggedUserDetails .getUserRoles ().equals ("ADMIN" ))) {
59+ throw new ForbiddenException ("User is not authorized to perform this operation" );
60+ } else {
61+ Optional <Stream > byId = streamRepository .findById (id );
62+ if (byId .isPresent ()) {
63+ streamRepository .deleteById (id );
64+ return "Stream deleted successfully" ;
65+ } else {
66+ return "Stream not found with id: " + id ;
67+ }
68+ }
69+ } catch (Exception e ) {
70+ throw new RuntimeException (e );
71+ }
72+ }
73+
74+ @ Override
75+ @ Transactional
76+ public String updateStream (UUID id , String stream ) {
77+ try {
78+ UserResponseDto loggedUserDetails = userDetailService .getLoggedUserDetails (UserUtil .extractToken ());
79+ if (loggedUserDetails == null ) {
80+ throw new ValueNotExistException ("User not found" );
81+ }
82+ if (!(loggedUserDetails .getUserRoles ().equals ("ADMIN" ))) {
83+ throw new ForbiddenException ("User is not authorized to perform this operation" );
84+ } else {
85+ Optional <Stream > byId = streamRepository .findById (id );
86+ if (byId .isPresent ()) {
87+ Stream stream1 = byId .get ();
88+ stream1 .setStream (stream );
89+ streamRepository .save (stream1 );
90+ return "Stream updated successfully" ;
91+ } else {
92+ return "Stream not found with id: " + id ;
93+ }
94+ }
95+ } catch (Exception e ) {
96+ throw new RuntimeException (e );
97+ }
98+ }
99+
100+ @ Override
101+ @ Transactional
102+ public StreamResponseDto getStreamById (UUID id ) throws ValueNotExistException {
103+ Optional <Stream > byId = streamRepository .findById (id );
104+ if (byId .isPresent ()) {
105+ Stream stream1 = byId .get ();
106+ return StreamMapper .INSTANCE .toDto (stream1 );
107+ } else {
108+ throw new ValueNotExistException ("Stream not found with id: " + id );
109+ }
110+ }
111+
112+ @ Override
113+ @ Transactional
114+ public List <StreamResponseDto > getAllStreams () throws ValueNotExistException {
115+ List <Stream > all = streamRepository .findAll ();
116+ if (all .isEmpty ()) {
117+ throw new ValueNotExistException ("No streams found" );
118+ } else {
119+ return StreamMapper .INSTANCE .toDtoList (all );
120+ }
121+ }
122+ }
0 commit comments