Skip to content

Commit

Permalink
Merge pull request #7 from Hangar-Tech/developer
Browse files Browse the repository at this point in the history
Developer
  • Loading branch information
MatheusVict authored Mar 6, 2024
2 parents 2b55f7f + 57b6597 commit 77d08e9
Show file tree
Hide file tree
Showing 97 changed files with 4,050 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
HELP.md
pdf/
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
htmlReport

### STS ###
.apt_generated
Expand Down
8 changes: 4 additions & 4 deletions mvnw.cmd
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM or more contributor license agreements. See the NOTICE fileEntity
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM regarding copyright ownership. The ASF licenses this fileEntity
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM "License"); you may not use this fileEntity except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM https://www.apache.org/licenses/LICENSE-2.0
Expand Down Expand Up @@ -153,7 +153,7 @@ if exist %WRAPPER_JAR% (
)
@REM End of extension

@REM If specified, validate the SHA-256 sum of the Maven wrapper jar file
@REM If specified, validate the SHA-256 sum of the Maven wrapper jar fileEntity
SET WRAPPER_SHA_256_SUM=""
FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
IF "%%A"=="wrapperSha256Sum" SET WRAPPER_SHA_256_SUM=%%B
Expand Down
47 changes: 46 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<description>sempre-alerta</description>
<properties>
<java.version>17</java.version>
<springdoc-openapi-starter-webmvc-ui.version>2.3.0</springdoc-openapi-starter-webmvc-ui.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -37,11 +38,34 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi-starter-webmvc-ui.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
<version>${springdoc-openapi-starter-webmvc-ui.version}</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand All @@ -52,6 +76,23 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.slugify/slugify -->
<dependency>
<groupId>com.github.slugify</groupId>
<artifactId>slugify</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down Expand Up @@ -93,6 +134,10 @@
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<image>
<name>matheusvict/${project.artifactId}:${project.version}</name>
</image>
<pullPolicy>IF_NOT_PRESENT</pullPolicy>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.institutosemprealerta.semprealerta;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.servers.Server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@OpenAPIDefinition(servers = {@Server(url = "/", description = "Default server url")})
public class SempreAlertaApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.institutosemprealerta.semprealerta.application.controllers;

import com.institutosemprealerta.semprealerta.domain.ports.out.request.LoginDTO;
import com.institutosemprealerta.semprealerta.domain.ports.out.responses.LoginResponse;
import com.institutosemprealerta.semprealerta.domain.service.AuthenticationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/auth")
@Tag(name = "auth")
public class AuthenticationController {

private final AuthenticationService authenticationService;

public AuthenticationController(AuthenticationService authenticationService) {
this.authenticationService = authenticationService;
}

@PostMapping("/login")
@Operation(summary = "Login", description = "You can login with your email and password")

public ResponseEntity<?> login(@RequestBody @Valid LoginDTO loginRequestBody) {
LoginResponse token = authenticationService.login(loginRequestBody);
return ResponseEntity.ok(token);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.institutosemprealerta.semprealerta.application.controllers;

import com.institutosemprealerta.semprealerta.domain.service.StorageService;
import com.institutosemprealerta.semprealerta.domain.ports.out.responses.FileResponse;
import com.institutosemprealerta.semprealerta.swagger.annotations.BadRequestResponse;
import com.institutosemprealerta.semprealerta.swagger.annotations.CreatedResponse;
import com.institutosemprealerta.semprealerta.swagger.annotations.NotFoundResponse;
import com.institutosemprealerta.semprealerta.swagger.annotations.OkResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.io.IOException;
import java.net.URI;
import java.util.List;

@Controller
@RequestMapping("/api/v1/files")
@Tag(name = "Files", description = "Files management")
public class FilesStorageController {
private StorageService storageService;

public FilesStorageController(StorageService storageService) {
this.storageService = storageService;
}

@Operation(summary = "Faça o upload de um arquivo", description = "Upload de um arquivo para o servidor")
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@CreatedResponse
@BadRequestResponse
public ResponseEntity<String> uploadFile(@RequestPart("file") MultipartFile file, @RequestParam("file_type") String fileType) {

String fileName = storageService.store(file, fileType);

String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/api/v1/files/download/")
.path(fileName)
.toUriString();

URI uri = URI.create(fileDownloadUri);
return ResponseEntity.created(uri).body("File uploaded successfully, file name: " + fileName + " on path: " + fileDownloadUri);
}

@GetMapping("/download/{fileName:.+}")
@ResponseBody
@Operation(summary = "Download de um arquivo", description = "Baixe um arquivo pelo nome do arquivo")
@OkResponse
@NotFoundResponse
@BadRequestResponse
public ResponseEntity<Resource> downloadFile(
@PathVariable String fileName,
HttpServletRequest request
) {

try {
Resource resource = storageService.loadAsResource(fileName);
String contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());

if (contentType == null) {
contentType = MediaType.APPLICATION_OCTET_STREAM.getType();
}

return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@GetMapping("/list")
@Operation(summary = "List todos os arquivos", description = "Liste todos os arquivos do servidor")
@OkResponse
@BadRequestResponse
public ResponseEntity<List<FileResponse>> listFiles() throws IOException {
List<FileResponse> fileNames = storageService.loadAll();

return ResponseEntity.ok(fileNames);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.institutosemprealerta.semprealerta.application.controllers;

import com.institutosemprealerta.semprealerta.domain.service.PostService;
import com.institutosemprealerta.semprealerta.domain.model.Post;
import com.institutosemprealerta.semprealerta.swagger.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;

@RestController
@RequestMapping("api/v1/posts")
@Tag(name = "Post", description = "Post management")
public class PostController {
private final PostService postService;

public PostController(PostService postService) {
this.postService = postService;
}

@GetMapping
@Operation(summary = "Lista de todos os posts", description = "Lista de todos os posts com paginação")
@OkResponse
public ResponseEntity<Page<Post>> getAllPosts(Pageable pageable) {
return ResponseEntity.ok(postService.listAll(pageable));
}

@PostMapping
@Operation(summary = "Criar postagem", description = "Crie uma nova postagem")
@CreatedResponse
@BadRequestResponse
public ResponseEntity<?> createPost(@Valid @RequestBody Post post) {
String slug = postService.save(post);
return ResponseEntity.created(URI.create("/api/v1/posts/" + slug)).build();
}

@GetMapping("/{slug}")
@Operation(summary = "Pegar post pelo slug", description = "Procura um post pelo seu slug")
@OkResponse
@NotFoundResponse
public ResponseEntity<Post> getPostBySlug(@PathVariable String slug) {
return ResponseEntity.ok(postService.findBySlug(slug));
}

@PutMapping("/{id}")
@Operation(summary = "Atualizar post", description = "Atualize um post existente pelo seu id")
@NoContentResponse
@NotFoundResponse
@BadRequestResponse
public ResponseEntity<?> updatePost(@PathVariable Long id, @Valid @RequestBody Post post) {
postService.update(id, post);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/{id}")
@Operation(summary = "Deletar post", description = "Deleta um post existente pelo seu id")
@NoContentResponse
@NotFoundResponse
public ResponseEntity<?> deletePost(@PathVariable Long id) {
postService.delete(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.institutosemprealerta.semprealerta.application.controllers;

import com.institutosemprealerta.semprealerta.domain.service.UserService;
import com.institutosemprealerta.semprealerta.domain.model.UserDTO;
import com.institutosemprealerta.semprealerta.domain.ports.out.responses.UserResponse;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User;
import com.institutosemprealerta.semprealerta.swagger.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/v1/user")
@Tag(name = "User", description = "Administração de usuários")
public class UserController {

private final UserService userService;

public UserController(UserService userService) {
this.userService = userService;
}

@PostMapping
@Operation(summary = "Criação de um usuário", description = "Criação de um usuário no sistema")
@CreatedResponse
@ConflictResponse
public ResponseEntity<?> createUser(@Valid @RequestBody UserDTO user) {
userService.save(user.toDomain());
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@GetMapping("/{id}")
@Operation(summary = "Busca de um usuário", description = "Busca de um usuário pelo id")
@OkResponse
@NotFoundResponse
public ResponseEntity<UserResponse> findById(@PathVariable int id) {
User userFound = userService.findById(id);
return ResponseEntity.ok().body(UserResponse.toResponse(userFound));
}

@GetMapping("/registration/{reg}")
@Operation(summary = "Busca de um usuário", description = "Busca de um usuário pela matrícula")
@OkResponse
@NotFoundResponse
public ResponseEntity<UserResponse> findByRegistration(@PathVariable String reg) {
User userFound = userService.findByRegistration(reg);
return ResponseEntity.ok().body(UserResponse.toResponse(userFound));
}

@PutMapping("/{id}")
@Operation(summary = "Atualização de um usuário", description = "Atualização de um usuário pelo id")
@NoContentResponse
@NotFoundResponse
@ConflictResponse
public ResponseEntity<?> updateUser(@PathVariable int id, @RequestBody UserDTO user) {
userService.update(id, user.toDomain());
return ResponseEntity.noContent().build();
}

@DeleteMapping("/{id}")
@Operation(summary = "Deleção de um usuário", description = "Deleção de um usuário pelo id")
@NoContentResponse
@NotFoundResponse
public ResponseEntity<?> deleteUser(@PathVariable int id) {
userService.delete(id);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}
Loading

0 comments on commit 77d08e9

Please sign in to comment.