Changeset 79a0317 for imaps-backend
- Timestamp:
- 01/21/25 03:08:24 (3 days ago)
- Branches:
- main
- Parents:
- 0c6b92a
- Location:
- imaps-backend
- Files:
-
- 145 added
- 3 deleted
- 21 edited
Legend:
- Unmodified
- Added
- Removed
-
imaps-backend/backendRender/pom.xml
r0c6b92a r79a0317 10 10 </parent> 11 11 <groupId>internettehnologii.imaps</groupId> 12 <artifactId> backendRender</artifactId>12 <artifactId>imaps-backend</artifactId> 13 13 <version>0.0.1-SNAPSHOT</version> 14 <packaging> war</packaging>15 <name> backendRender</name>16 <description>Backend api for rendering an indoor map</description>14 <packaging>jar</packaging> 15 <name>imaps-backend</name> 16 <description>Backend api for rendering indoor maps</description> 17 17 <url/> 18 18 <licenses> … … 121 121 </dependency> 122 122 <dependency> 123 <groupId>org.springframework.boot</groupId> 124 <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> 125 </dependency> 126 <dependency> 127 <groupId>org.springframework.boot</groupId> 128 <artifactId>spring-boot-starter-mail</artifactId> 129 <version>3.4.0</version> 130 </dependency> 131 132 <dependency> 123 133 <groupId>com.googlecode.json-simple</groupId> 124 134 <artifactId>json-simple</artifactId> … … 146 156 <scope>test</scope> 147 157 </dependency> 148 149 150 158 <dependency> 159 <groupId>org.springframework.boot</groupId> 160 <artifactId>spring-boot-starter-actuator</artifactId> 161 </dependency> 151 162 152 163 </dependencies> -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/BackendRenderApplication.java
r0c6b92a r79a0317 3 3 import org.springframework.boot.SpringApplication; 4 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.mail.javamail.JavaMailSender; 7 import org.springframework.mail.javamail.JavaMailSenderImpl; 8 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 9 import org.springframework.security.crypto.password.PasswordEncoder; 5 10 6 11 @SpringBootApplication … … 11 16 } 12 17 18 @Bean 19 public PasswordEncoder passwordEncoder(){ 20 return new BCryptPasswordEncoder(12); 21 } 22 13 23 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/controllers/AdminController.java
r0c6b92a r79a0317 4 4 import internettehnologii.imaps.backendRender.web.service.interfaces.MapService; 5 5 import internettehnologii.imaps.backendRender.web.service.interfaces.PublishRequestService; 6 import internettehnologii.imaps.backendRender.web.service.interfaces.ReportService; 6 7 import internettehnologii.imaps.backendRender.web.util.DTO.MapDTO; 7 8 import internettehnologii.imaps.backendRender.web.util.DTO.PublishMapDTO; 9 import internettehnologii.imaps.backendRender.web.util.DTO.ReportDTO; 8 10 import internettehnologii.imaps.backendRender.web.util.Util; 9 11 import org.springframework.http.ResponseEntity; … … 23 25 private final MapService mapService; 24 26 private final PublishRequestService publishRequestService; 27 private final ReportService reportService; 25 28 26 public AdminController(MapService mapService, PublishRequestService publishRequestService ) {29 public AdminController(MapService mapService, PublishRequestService publishRequestService, ReportService reportService) { 27 30 this.mapService = mapService; 28 31 this.publishRequestService = publishRequestService; 32 this.reportService = reportService; 29 33 } 30 34 … … 32 36 public ResponseEntity<List<MapDTO>> getPendingMapRequests() { 33 37 try { 34 List<MapDTO> maps = Util.convertToMapDTO(mapService.findByStatus(MAP_STATUS. INVALID));38 List<MapDTO> maps = Util.convertToMapDTO(mapService.findByStatus(MAP_STATUS.PENDING)); 35 39 return ResponseEntity.ok(maps); 36 40 } catch (Exception e) { … … 71 75 } 72 76 @PostMapping("/pr/deny") 73 public ResponseEntity<Map<String,Object>> denyPR(@RequestParam("id") Integer id ) {77 public ResponseEntity<Map<String,Object>> denyPR(@RequestParam("id") Integer id,@RequestParam String reason) { 74 78 try{ 75 publishRequestService.denyPublishRequest(id );79 publishRequestService.denyPublishRequest(id,reason); 76 80 } catch (Exception e){ 77 81 e.printStackTrace(); … … 79 83 return ResponseEntity.ok(new HashMap<>()); 80 84 } 85 @GetMapping("/load-reports") 86 public ResponseEntity<List<ReportDTO>> getReports() { 87 try { 88 List<ReportDTO> reportDTOS = reportService.getReports() 89 .stream() 90 .map(report -> 91 new ReportDTO( 92 report.getUser().getUsername(), 93 report.getMap().getName(), 94 report.getSubject(), 95 report.getContent(), 96 report.getCreatedAt().toString() 97 ) 98 ).toList(); 99 return ResponseEntity.ok(reportDTOS); 100 } catch (Exception e) { 101 e.printStackTrace(); 102 return ResponseEntity.badRequest().build(); 103 } 104 } 81 105 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/controllers/AuthController.java
r0c6b92a r79a0317 23 23 @RequestMapping("/api/auth") 24 24 @RestController 25 @CrossOrigin(origins = "http://localhost:5173/")25 //@CrossOrigin(origins = "http://localhost:5173") 26 26 public class AuthController { 27 27 -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/controllers/MapDrawController.java
r0c6b92a r79a0317 52 52 53 53 @PostMapping("/publish/add") 54 public ResponseEntity<Map <String,Object>> sendPublishRequest(@RequestBody PublishMapDTO formData, @RequestParam String username) {54 public ResponseEntity<MapDTO> sendPublishRequest(@RequestBody PublishMapDTO formData, @RequestParam String username) { 55 55 56 56 System.out.println("FORM DATA: -------------------------------------------- " + formData); 57 try {57 try { 58 58 IMapsUser user = userService.getUser(username); 59 59 60 publishRequestService.addPublishRequest(formData,user); 61 62 return ResponseEntity.ok(new HashMap<>()); 63 64 } catch (Exception e){ 65 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); 66 } 67 } 60 MapDTO updatedMap = publishRequestService.addPublishRequest(formData, user); 61 62 return ResponseEntity.ok(updatedMap); 63 64 } catch (Exception e) { 65 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); 66 } 67 } 68 68 69 @GetMapping("/publish/get") 69 70 public ResponseEntity<PublishMapDTO> getPublishRequestForMap(@RequestParam String mapName) { 70 71 PublishRequest pr = publishRequestService.getPublishRequestByMapName(mapName); 71 PublishMapDTO dto = new PublishMapDTO(pr.getId(), pr.getName(), pr.getLastName(), pr.getMap().getName(), pr.getMapType(), pr.getGMapsUrl());72 PublishMapDTO dto = new PublishMapDTO(pr.getId(), pr.getName(), pr.getLastName(), pr.getMap().getName(), pr.getMapType(), pr.getGMapsUrl()); 72 73 return ResponseEntity.ok(dto); 73 74 } … … 76 77 public ResponseEntity<List<MapDTO>> getFavouriteMapsForUser(@RequestParam String username) { 77 78 78 try {79 try { 79 80 IMapsUser user = userService.getUser(username); 80 81 List<MapDTO> mapDTOS = Util.convertToMapDTO(user.getFavoriteMaps()); 81 82 return ResponseEntity.ok().body(mapDTOS); 82 83 83 } catch (Exception e) {84 } catch (Exception e) { 84 85 System.out.println(e.getMessage()); 85 86 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); … … 91 92 (@RequestBody SaveMapDTO saveMapDTO, @RequestParam String username) { 92 93 93 System.out.println("MAP DTO: " + saveMapDTO); 94 95 try { 96 97 System.out.println("================================="); 98 System.out.println("MAPDTO: " + saveMapDTO); 99 System.out.println("USERNAME:" + username); 94 95 try { 100 96 IndoorMap map = mapService.getMapForUser(username, saveMapDTO.getMapName()); 101 97 Floor f = floorService.getFloorByNum(saveMapDTO.getFloorNum(), map); … … 114 110 System.out.println("UPDATED FLOOR " + f.getFloorNumber()); 115 111 116 FloorDTO floorDTO = new FloorDTO(f.getFloorNumber(), saveMapDTO.getMapName(), f.getMapData().getShapeData());112 FloorDTO floorDTO = new FloorDTO(f.getFloorNumber(), saveMapDTO.getMapName(), f.getMapData().getShapeData()); 117 113 118 114 return ResponseEntity.ok(floorDTO); … … 126 122 @PutMapping("/my-maps/create") 127 123 public ResponseEntity<MapDTO> createMap(@RequestBody CreateMapDTO mapData, @RequestParam String username) { 128 try {129 mapService.createMap(mapData.getName(), mapData.getMapType() ,username);130 IndoorMap map = mapService.getMapForUser(username, mapData.getName());124 try { 125 mapService.createMap(mapData.getName(), username); 126 IndoorMap map = mapService.getMapForUser(username, mapData.getName()); 131 127 MapDTO mapDTO = new MapDTO(map.getName(), 132 map.getMapType() 133 ,map.getCreatedAt(),128 map.getMapType(), 129 map.getCreatedAt(), 134 130 map.getModifiedAt(), 135 131 map.getStatus().name(), … … 139 135 } catch (Exception e) { 140 136 System.out.println(e.getMessage()); 141 e.printStackTrace();142 137 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); 143 138 } … … 147 142 @GetMapping("/my-maps/load") 148 143 public ResponseEntity<List<FloorDTO>> loadPersonalMap(@RequestParam String mapName, @RequestParam String username) { 149 try {150 IndoorMap map = mapService.getMapForUser(username, mapName);144 try { 145 IndoorMap map = mapService.getMapForUser(username, mapName); 151 146 List<Floor> floors = map.getFloors(); 152 147 return ResponseEntity.ok().body(Util.convertToFloorDTO(floors)); … … 158 153 159 154 @PostMapping("/room-types/add") 160 public ResponseEntity<Map<String, Object>> addRoomType(@RequestParam String roomTypeName,161 @RequestParam String username,162 @RequestParam String mapName) {163 try { 164 IndoorMap map = mapService.getMapForUser(username, mapName);165 roomTypeService.addRoomType(roomTypeName, map);155 public ResponseEntity<Map<String, Object>> addRoomType(@RequestParam String roomTypeName, 156 @RequestParam String username, 157 @RequestParam String mapName) { 158 try { 159 IndoorMap map = mapService.getMapForUser(username, mapName); 160 roomTypeService.addRoomType(roomTypeName, map); 166 161 return ResponseEntity.ok(new HashMap<>()); 167 162 168 } catch (Exception e) {163 } catch (Exception e) { 169 164 System.out.println(e.getMessage()); 170 165 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); … … 175 170 public ResponseEntity<List<RoomTypeDTO>> getRoomTypesForMap(@RequestParam String mapName, @RequestParam String username) { 176 171 177 IndoorMap map = mapService.getMapForUser(username, mapName);172 IndoorMap map = mapService.getMapForUser(username, mapName); 178 173 179 174 List<RoomTypeDTO> roomTypeDTOS = map.getRoomTypes().stream().map(r -> new RoomTypeDTO(r.getName())).toList(); … … 182 177 183 178 @DeleteMapping("/floors/delete") 184 public ResponseEntity<Map<String, Object>> deleteFloor(@RequestParam String mapName,185 @RequestParam int floorNum) {186 try {187 floorService.deleteFloor(floorNum, mapName);179 public ResponseEntity<Map<String, Object>> deleteFloor(@RequestParam String mapName, 180 @RequestParam int floorNum) { 181 try { 182 floorService.deleteFloor(floorNum, mapName); 188 183 return ResponseEntity.ok(new HashMap<>()); 189 } catch (Exception e) {184 } catch (Exception e) { 190 185 System.out.println(e.getMessage()); 191 186 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); … … 194 189 195 190 @PutMapping("/floors/add") 196 public ResponseEntity<Map<String, Object>> addFloor(@RequestBody FloorDTO floorDTO) {197 Map<String, Object> response = new HashMap<>();198 try {199 floorService.addFloor(floorDTO.getNum(), floorDTO.getMapName());191 public ResponseEntity<Map<String, Object>> addFloor(@RequestBody FloorDTO floorDTO) { 192 Map<String, Object> response = new HashMap<>(); 193 try { 194 floorService.addFloor(floorDTO.getNum(), floorDTO.getMapName()); 200 195 return ResponseEntity.ok(response); 201 196 } catch (Exception e) { … … 226 221 227 222 223 @PostMapping("/my-maps/edit") 224 public ResponseEntity<MapDTO> updateMapInfo(@RequestBody EditMapDTO editMapDTO) { 225 226 227 228 try { 229 230 MapDTO mapDTO = mapService.updateMapInfo(editMapDTO); 231 232 233 return new ResponseEntity<>(mapDTO, HttpStatus.OK); 234 235 } catch (Exception e) { 236 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); 237 } 238 239 } 228 240 229 241 -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/controllers/MapViewController.java
r0c6b92a r79a0317 6 6 import internettehnologii.imaps.backendRender.web.entities.IMapsUser; 7 7 import internettehnologii.imaps.backendRender.web.entities.IndoorMap; 8 import internettehnologii.imaps.backendRender.web.entities.Report; 8 9 import internettehnologii.imaps.backendRender.web.exceptions.EmptyMapException; 9 import internettehnologii.imaps.backendRender.web.exceptions.FloorNotFoundException; 10 import internettehnologii.imaps.backendRender.web.service.interfaces.GraphService; 11 import internettehnologii.imaps.backendRender.web.service.interfaces.FloorService; 12 import internettehnologii.imaps.backendRender.web.service.interfaces.MapService; 13 import internettehnologii.imaps.backendRender.web.service.interfaces.UserService; 10 import internettehnologii.imaps.backendRender.web.service.interfaces.*; 14 11 import internettehnologii.imaps.backendRender.web.util.DTO.FloorDTO; 15 12 import internettehnologii.imaps.backendRender.web.util.DTO.MapDTO; 13 import internettehnologii.imaps.backendRender.web.util.DTO.ReportDTO; 16 14 import internettehnologii.imaps.backendRender.web.util.DTO.RoomTypeDTO; 17 15 import internettehnologii.imaps.backendRender.web.util.Util; … … 34 32 private final GraphService graphService; 35 33 private final UserService userService; 34 private final ReportService reportService; 36 35 37 36 @Autowired 38 public MapViewController(MapService mapService, FloorService floorService, GraphService graphService, UserService userService ) {37 public MapViewController(MapService mapService, FloorService floorService, GraphService graphService, UserService userService, ReportService reportService) { 39 38 this.mapService = mapService; 40 39 this.floorService = floorService; 41 40 this.graphService = graphService; 42 41 this.userService = userService; 42 this.reportService = reportService; 43 43 } 44 44 … … 120 120 IndoorMap map = mapService.getMapByName(mapName); 121 121 IMapsUser user = userService.getUser(username); 122 userService.add ToFavorites(user, map);122 userService.addFavoriteMap(user, map); 123 123 return ResponseEntity.ok(new HashMap<>()); 124 124 } catch (Exception e){ … … 132 132 IndoorMap map = mapService.getMapByName(mapName); 133 133 IMapsUser user = userService.getUser(username); 134 userService.removeF romFavorites(user, map);134 userService.removeFavoriteMap(user, map); 135 135 return ResponseEntity.ok(new HashMap<>()); 136 136 } catch (Exception e){ … … 139 139 } 140 140 } 141 @PostMapping("/protected/reports/create") 142 public ResponseEntity<Map<String, Object>> createReport( 143 @RequestBody ReportDTO reportBody) { 144 try { 145 IMapsUser user = userService.getUser(reportBody.getUsername()); 146 IndoorMap map = mapService.getMapByName(reportBody.getMapName()); 147 Report report = new Report(user, map, reportBody.getSubject(), reportBody.getContent()); 148 reportService.saveReport(report); 141 149 150 Map<String, Object> response = new HashMap<>(); 151 return ResponseEntity.ok(response); 152 } catch (Exception e) { 153 e.printStackTrace(); 154 return ResponseEntity.status(404).body(new HashMap<>()); 155 } 156 } 142 157 143 158 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/entities/IMapsUser.java
r0c6b92a r79a0317 30 30 @Column(name = "profile_image_url") 31 31 private String profileImageUrl; 32 @Column(name = "oauth_provider") 33 private String OAuthProvider; 34 @Column(name = "oauth_id") 35 private String oAuthId; 36 @Column(name = "access_token") 37 private String accessToken; 38 @Column(name = "refresh_token") 39 private String refreshToken; 32 40 33 41 @ManyToMany -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/entities/IndoorMap.java
r0c6b92a r79a0317 1 1 package internettehnologii.imaps.backendRender.web.entities; 2 import internettehnologii.imaps.backendRender.web.util.DTO.MapDTO; 2 3 import jakarta.persistence.*; 3 4 import lombok.Getter; … … 44 45 private Boolean isPublished; 45 46 47 46 48 @ManyToOne 47 49 @JoinColumn(name = "usr_id",referencedColumnName = "id", nullable = false) … … 70 72 } 71 73 74 75 public MapDTO toMapDTO(){ 76 return new MapDTO(getName(), 77 getMapType(), 78 getCreatedAt(), 79 getModifiedAt(), 80 getStatus().name(), 81 getFavouriteCount(), 82 getGmapsUrl()); 83 } 84 72 85 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/entities/MAP_STATUS.java
r0c6b92a r79a0317 4 4 PUBLIC, 5 5 PRIVATE, 6 INVALID,6 PENDING, 7 7 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/repo/FloorRepository.java
r0c6b92a r79a0317 22 22 @Query("SELECT COUNT(f) > 0 FROM Floor f WHERE f.floorNumber = ?1 AND f.indoorMap = ?2") 23 23 boolean existsFloorForMap(Integer floorNumber, IndoorMap indoorMap); 24 void deleteFloorByFloorNumberAndIndoorMap(Integer floorNumber,IndoorMap indoorMap); 24 25 25 void deleteFloorByFloorNumberAndIndoorMap(Integer floorNumber,IndoorMap indoorMap);26 26 Optional<Floor> findFloorByFloorNumberAndIndoorMap(Integer floorNumber,IndoorMap indoorMap); 27 27 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/repo/UserRepository.java
r0c6b92a r79a0317 6 6 import org.springframework.stereotype.Repository; 7 7 8 import java.util.List; 8 9 import java.util.Optional; 9 10 … … 25 26 value = "SELECT * FROM users u WHERE u.id= ?1") 26 27 Optional<IMapsUser> getUserById(Long usrid); 28 @Query(value = "FROM IMapsUser u WHERE u.oAuthId = ?1") 29 Optional<IMapsUser> getUserByOAuthId(String oauthid); 30 @Query(nativeQuery = true, 31 value = "SELECT u.* FROM users u LEFT JOIN users_roles ur on u.id = ur.user_id where ur.role_id = 3;") 32 Optional<List<IMapsUser>> getAllAdmins(); 27 33 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/security/JwtFilter.java
r0c6b92a r79a0317 37 37 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 38 38 System.out.println(request.getRequestURI()); 39 if(request.getRequestURI().contains("/login") || request.getRequestURI().contains("/register")) { 39 if( 40 request.getRequestURI().contains("/login") || 41 request.getRequestURI().contains("/register") || 42 request.getMethod().equals("OPTIONS") 43 ) { 40 44 filterChain.doFilter(request, response); 41 45 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/security/WebSecurityConfig.java
r0c6b92a r79a0317 5 5 import org.springframework.context.annotation.Bean; 6 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.Primary; 8 import org.springframework.context.annotation.Profile; 7 9 import org.springframework.security.authentication.AuthenticationManager; 8 10 import org.springframework.security.authentication.AuthenticationProvider; … … 15 17 import org.springframework.security.config.http.SessionCreationPolicy; 16 18 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 19 import org.springframework.security.crypto.password.PasswordEncoder; 17 20 import org.springframework.security.web.SecurityFilterChain; 18 21 import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; … … 28 31 private MapUserDetailsService userDetailsService; 29 32 33 private final PasswordEncoder passwordEncoder; 34 30 35 @Autowired 31 36 private JwtFilter jwtFilter; 37 private final CorsConfigurationImpl corsConfiguration; 38 39 public WebSecurityConfig(PasswordEncoder passwordEncoder, CorsConfigurationImpl corsConfiguration) { 40 this.passwordEncoder = passwordEncoder; 41 this.corsConfiguration = corsConfiguration; 42 } 32 43 33 44 @Bean … … 35 46 http 36 47 .csrf(AbstractHttpConfigurer::disable) 37 .cors( Customizer.withDefaults())48 .cors(cors -> cors.configurationSource(corsConfiguration.corsConfigurationSourceDev())) 38 49 .authorizeHttpRequests(request -> 39 request.requestMatchers("/protected/**") 40 .hasRole("USER") 41 .requestMatchers("/api/admin/**") 42 .hasRole("ADMIN") 43 .anyRequest() 44 .permitAll()) 50 request 51 .requestMatchers("/protected/**").hasRole("USER") 52 .requestMatchers("/api/admin/**").hasRole("ADMIN") 53 .anyRequest().permitAll()) 54 45 55 .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) 46 56 .addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class); 47 48 57 49 58 return http.build(); … … 51 60 52 61 53 @Bean54 public CorsConfigurationSource corsConfigurationSource() {55 CorsConfiguration configuration = new CorsConfiguration();56 configuration.addAllowedOrigin("http://localhost:5173/");57 configuration.addAllowedMethod("*");58 configuration.addAllowedHeader("*");59 configuration.addAllowedHeader("*");60 configuration.setAllowCredentials(true);61 62 UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();63 source.registerCorsConfiguration("/**", configuration);64 65 return source;66 }67 62 68 63 … … 70 65 public AuthenticationProvider authenticationProvider() { 71 66 DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); 72 daoAuthenticationProvider.setPasswordEncoder( new BCryptPasswordEncoder(12));67 daoAuthenticationProvider.setPasswordEncoder(passwordEncoder); 73 68 daoAuthenticationProvider.setUserDetailsService(userDetailsService); 74 69 return daoAuthenticationProvider; -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/service/impl/MapServiceImpl.java
r0c6b92a r79a0317 1 1 package internettehnologii.imaps.backendRender.web.service.impl; 2 2 3 import internettehnologii.imaps.backendRender.web.entities.*; 3 import internettehnologii.imaps.backendRender.web.entities.Floor; 4 import internettehnologii.imaps.backendRender.web.entities.IMapsUser; 5 import internettehnologii.imaps.backendRender.web.entities.IndoorMap; 6 import internettehnologii.imaps.backendRender.web.entities.MAP_STATUS; 4 7 import internettehnologii.imaps.backendRender.web.exceptions.MapNameTakenException; 5 8 import internettehnologii.imaps.backendRender.web.exceptions.MapNotFoundException; … … 8 11 import internettehnologii.imaps.backendRender.web.repo.UserRepository; 9 12 import internettehnologii.imaps.backendRender.web.service.interfaces.MapService; 13 import internettehnologii.imaps.backendRender.web.util.DTO.EditMapDTO; 14 import internettehnologii.imaps.backendRender.web.util.DTO.MapDTO; 15 import jakarta.transaction.Transactional; 10 16 import org.springframework.beans.factory.annotation.Autowired; 11 17 import org.springframework.security.core.userdetails.UsernameNotFoundException; … … 31 37 32 38 @Override 33 public void createMap(String mapName, String mapType,String username) {39 public void createMap(String mapName, String username) { 34 40 35 41 Optional<IMapsUser> user = userRepository.findUserByName(username); … … 38 44 throw new MapNameTakenException("Map with name " + mapName + " already exists"); 39 45 40 if (user.isEmpty()){46 if (user.isEmpty()) { 41 47 throw new UsernameNotFoundException("User " + username + " not found"); 42 48 } … … 58 64 public void updateMap(IndoorMap indoorMap) { 59 65 mapRepository.save(indoorMap); 66 } 67 68 @Transactional 69 @Override 70 public MapDTO updateMapInfo(EditMapDTO editMapDTO) { 71 IndoorMap indoorMap = mapRepository.getIndoorMapByName(editMapDTO.getInitialName()).orElseThrow(() -> new MapNotFoundException("Map with name " + editMapDTO.getInitialName() + " not found")); 72 if (mapRepository.existsByName(editMapDTO.getName()) && !Objects.equals(editMapDTO.getInitialName(), editMapDTO.getName())) { 73 throw new MapNameTakenException("Map with name " + editMapDTO.getName() + " already exists"); 74 } 75 76 indoorMap.setName(editMapDTO.getName()); 77 indoorMap.setGmapsUrl(editMapDTO.getGmapsUrl()); 78 // indoorMap.setMapType(editMapDTO.getType()); //TODO 79 mapRepository.save(indoorMap); 80 return indoorMap.toMapDTO(); 60 81 } 61 82 … … 85 106 boolean isAdmin = user.getRoles().stream().anyMatch(role -> Objects.equals(role.getName(), "ADMIN")); 86 107 87 if (isAdmin){108 if (isAdmin) { 88 109 return map; 89 110 } … … 99 120 @Override 100 121 public IndoorMap getMapByName(String mapName) { 101 return mapRepository.getIndoorMapByName(mapName).orElseThrow(() -> new MapNotFoundException(mapName));122 return mapRepository.getIndoorMapByName(mapName).orElseThrow(() -> new MapNotFoundException(mapName)); 102 123 } 103 124 -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/service/impl/PublishRequestServiceImpl.java
r0c6b92a r79a0317 7 7 import internettehnologii.imaps.backendRender.web.repo.MapRepository; 8 8 import internettehnologii.imaps.backendRender.web.repo.PublishRequestRepository; 9 import internettehnologii.imaps.backendRender.web.repo.UserRepository; 9 10 import internettehnologii.imaps.backendRender.web.service.interfaces.MapService; 10 11 import internettehnologii.imaps.backendRender.web.service.interfaces.PublishRequestService; 11 12 import internettehnologii.imaps.backendRender.web.service.interfaces.UserService; 13 import internettehnologii.imaps.backendRender.web.util.DTO.MapDTO; 12 14 import internettehnologii.imaps.backendRender.web.util.DTO.PublishMapDTO; 15 import jakarta.transaction.Transactional; 16 import org.springframework.mail.SimpleMailMessage; 17 import org.springframework.mail.javamail.JavaMailSender; 13 18 import org.springframework.stereotype.Service; 14 19 … … 22 27 private final MapService mapService; 23 28 private final UserService userService; 29 //private final JavaMailSender mailSender; 30 private final UserRepository userRepository; 24 31 25 public PublishRequestServiceImpl(PublishRequestRepository publishRequestRepository, MapRepository mapRepository, MapService mapService, UserService userService ) {32 public PublishRequestServiceImpl(PublishRequestRepository publishRequestRepository, MapRepository mapRepository, MapService mapService, UserService userService, UserRepository userRepository) { 26 33 this.publishRequestRepository = publishRequestRepository; 27 34 this.mapRepository = mapRepository; 28 35 this.mapService = mapService; 29 36 this.userService = userService; 37 this.userRepository = userRepository; 30 38 } 31 39 32 40 @Override 33 public void addPublishRequest(PublishMapDTO formData, IMapsUser user) { 41 @Transactional 42 public MapDTO addPublishRequest(PublishMapDTO formData, IMapsUser user) { 34 43 Optional<PublishRequest> prOptional = publishRequestRepository.findById(formData.getId()); 35 44 PublishRequest pr; … … 41 50 pr.setGMapsUrl(formData.getGoogleMapsUrl()); 42 51 pr.setMapType(formData.getMapType()); 43 map.setStatus(MAP_STATUS. INVALID);52 map.setStatus(MAP_STATUS.PENDING); 44 53 } else { 45 54 pr = new PublishRequest(formData.getName(), formData.getLastName(), formData.getGoogleMapsUrl(), formData.getMapType()); 46 47 55 pr.setMap(map); 48 56 pr.setUser(user); 49 map.setStatus(MAP_STATUS. INVALID);57 map.setStatus(MAP_STATUS.PENDING); 50 58 51 59 } 52 60 this.mapRepository.save(map); 53 61 this.publishRequestRepository.save(pr); 62 sendMailToAdmins(user,pr); 63 return map.toMapDTO(); 54 64 55 65 } 56 66 57 67 @Override 58 public void denyPublishRequest(int id ) throws Exception {68 public void denyPublishRequest(int id,String reason) throws Exception { 59 69 PublishRequest pr = this.publishRequestRepository.findById(id).orElseThrow(Exception::new); 60 70 … … 66 76 this.publishRequestRepository.save(pr); 67 77 this.mapRepository.save(map); 78 79 // SimpleMailMessage message = new SimpleMailMessage(); 80 // 81 // message.setTo(pr.getMap().getUser().getEmail()); 82 // message.setSubject("Denied Publish Request"); 83 // message.setText(String.format("Your publish request for map: %s has been denied." + 84 // "\nReason:\n%s",pr.getMap().getName(),reason)); 85 // mailSender.send(message); 68 86 } 69 87 … … 77 95 78 96 } 97 @Override 98 public void sendMailToAdmins(IMapsUser user, PublishRequest pr){ 99 // System.out.println("test"); 100 // SimpleMailMessage message = new SimpleMailMessage(); 101 // Optional<List<IMapsUser>> adminsOpt = userRepository.getAllAdmins(); 102 // adminsOpt.ifPresent(admins -> { 103 // admins.forEach(admin -> { 104 // message.setTo(admin.getEmail()); 105 // message.setSubject("New Publish Request"); 106 // String body = String.format( 107 // """ 108 // Publish Request Details: 109 // \tRequest Id: %s 110 // \tMap Id: %s 111 // \tMap Name: %s 112 // 113 // User Details: 114 // \tId: %s 115 // \tUsername: %s 116 // \tEmail: %s 117 // \tFirst Name: %s 118 // \t Last Name: %s 119 // """, 120 // pr.getId(), pr.getMap().getId(), 121 // pr.getMap().getName(), 122 // user.getId(),user.getUsername(),user.getEmail(), 123 // pr.getName(),pr.getLastName()); 124 // 125 // message.setText(body); 126 // mailSender.send(message); 127 // }); 128 // }); 79 129 80 // todo mail pustanje 130 } 131 81 132 82 133 @Override … … 93 144 this.publishRequestRepository.save(pr); 94 145 this.mapRepository.save(map); 146 147 // SimpleMailMessage message = new SimpleMailMessage(); 148 // message.setTo(pr.getMap().getUser().getEmail()); 149 // message.setSubject("Approved Publish Request"); 150 // message.setText(String.format("Your publish request for map: %s has been approved!\nYour map is now public. That means that anyone can view and use your map.\nThank you for building!",pr.getMap().getName())); 151 // mailSender.send(message); 152 95 153 } 96 154 -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/service/impl/UserServiceImpl.java
r0c6b92a r79a0317 21 21 import org.springframework.security.core.userdetails.UserDetails; 22 22 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 23 import org.springframework.security.crypto.password.PasswordEncoder; 23 24 import org.springframework.stereotype.Service; 24 25 … … 31 32 private final UserRepository userRepository; 32 33 33 private final BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(12); 34 @Autowired 35 private PasswordEncoder passwordEncoder; 36 34 37 @Autowired 35 38 private JWTService jwtService; … … 56 59 }); 57 60 58 user.setPassword( bCryptPasswordEncoder.encode(user.getPassword()));61 user.setPassword(passwordEncoder.encode(user.getPassword())); 59 62 60 63 Role userRole = roleRepository.findByName("USER").orElseThrow(RoleNotFoundException::new); … … 84 87 85 88 @Override 86 public void add ToFavorites(IMapsUser user, IndoorMap map) {89 public void addFavoriteMap(IMapsUser user, IndoorMap map) { 87 90 user.getFavoriteMaps().add(map); 88 91 userRepository.save(user); … … 90 93 91 94 @Override 92 public void removeF romFavorites(IMapsUser user, IndoorMap map) {95 public void removeFavoriteMap(IMapsUser user, IndoorMap map) { 93 96 boolean removed = user.getFavoriteMaps().remove(map); 94 97 if(!removed) throw new MapNotFoundException("Map not found. " + map.getName()); … … 104 107 105 108 106 107 109 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/service/interfaces/MapService.java
r0c6b92a r79a0317 4 4 import internettehnologii.imaps.backendRender.web.entities.MAP_STATUS; 5 5 import internettehnologii.imaps.backendRender.web.entities.RoomType; 6 import internettehnologii.imaps.backendRender.web.util.DTO.EditMapDTO; 7 import internettehnologii.imaps.backendRender.web.util.DTO.MapDTO; 6 8 7 9 import java.util.List; 8 10 9 11 public interface MapService { 10 void createMap(String mapName, String mapType, Stringusername);12 void createMap(String mapName, String username); 11 13 void updateMap(IndoorMap indoorMap); 14 MapDTO updateMapInfo(EditMapDTO editMapDTO); 12 15 void deleteMap(IndoorMap indoorMap); 13 16 List<IndoorMap> getAllMapsForUser(String username); -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/service/interfaces/PublishRequestService.java
r0c6b92a r79a0317 3 3 import internettehnologii.imaps.backendRender.web.entities.IMapsUser; 4 4 import internettehnologii.imaps.backendRender.web.entities.PublishRequest; 5 import internettehnologii.imaps.backendRender.web.util.DTO.MapDTO; 5 6 import internettehnologii.imaps.backendRender.web.util.DTO.PublishMapDTO; 6 7 … … 8 9 9 10 public interface PublishRequestService { 10 voidaddPublishRequest(PublishMapDTO publishRequestDTO, IMapsUser user);11 void denyPublishRequest(int id ) throws Exception;11 MapDTO addPublishRequest(PublishMapDTO publishRequestDTO, IMapsUser user); 12 void denyPublishRequest(int id,String reason) throws Exception; 12 13 List<PublishRequest> getAllPublishRequests(); 13 14 void save(PublishRequest publishRequest); … … 16 17 PublishRequest getPublishRequestByMapName(String mapName); 17 18 void editPublishRequest(PublishRequest publishRequest); 19 void sendMailToAdmins(IMapsUser user,PublishRequest pr); 18 20 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/service/interfaces/UserService.java
r0c6b92a r79a0317 7 7 8 8 import javax.management.relation.RoleNotFoundException; 9 import javax.naming.AuthenticationException;10 9 11 10 public interface UserService { 12 11 IMapsUser register(IMapsUser user) throws RoleNotFoundException; 13 12 UserAuthSuccessDTO login(UserLoginDTO user) throws Exception; 14 void add ToFavorites(IMapsUser user, IndoorMap map);15 void removeF romFavorites(IMapsUser user, IndoorMap map);13 void addFavoriteMap(IMapsUser user, IndoorMap map); 14 void removeFavoriteMap(IMapsUser user, IndoorMap map); 16 15 IMapsUser getUser(String username); 17 16 } -
imaps-backend/backendRender/src/main/java/internettehnologii/imaps/backendRender/web/util/DTO/CreateMapDTO.java
r0c6b92a r79a0317 10 10 public class CreateMapDTO { 11 11 private String name; 12 p rivate String mapType;12 public CreateMapDTO() {} 13 13 } -
imaps-backend/backendRender/src/main/resources/application.properties
r0c6b92a r79a0317 1 spring.application.name=backendRender 2 spring.datasource.url=jdbc:postgresql://localhost:5432/imaps_prod 1 spring.application.name=imaps-backend 3 2 spring.datasource.username=${DB_USER} 4 3 spring.datasource.password=${DB_PASS} … … 8 7 spring.jpa.properties.hibernate.format_sql=true 9 8 9 10 11 spring.mail.host=smtp.gmail.com 12 spring.mail.port=587 13 spring.mail.username=${MAIL_NAME} 14 spring.mail.password=${MAIL_PASS} 15 spring.mail.properties.mail.smtp.auth=true 16 spring.mail.properties.mail.smtp.starttls.enable=true 17 18 spring.profiles.active=dev 10 19 logging.level.org.springframework.web=DEBUG 11 20
Note:
See TracChangeset
for help on using the changeset viewer.