Compare commits

..

No commits in common. '474bb9daaf1ea54b2b6212db86f18ac1b6395065' and 'fb307cfba8b800383356c36eeba998d6f034ccdd' have entirely different histories.

@ -30,7 +30,7 @@ steps:
base_url:
from_secret: BASE_URL
files:
- target/*.war
- target/*.jar
checksum:
- md5
- sha1

@ -24,22 +24,27 @@ Type: build
```shell
./mvnw clean install -DskipTests=true
# or
make war
./mvnw clean package
```
Type: preparation
- create a folder, for example, `project`.
- copy `SpringRestExample.war` from a release to `target/` in the folder.
- if you build the application from source, skip this step.
- copy `Dockerfile` and `docker-compose.yml` to the folder.
- turn up the project in the `project`:
- copy `spring_rest_example-version.jar` from a release to the folder.
- if you build the application from source, find the file in `target/`.
- copy docker/docker-compose.yml to the folder.
- turn up the database in the `project/docker`:
```
docker-compose up -d --build
docker-compose up -d
```
- wait a minute while the database is bootstrapping.
- now you can connect to the application.
- now you can connect with the application.
Type: run
After the preparation, run the application in the folder, for example, `project`:
```shell
java -jar spring_rest_example-2.0.1.jar
```
## Author

@ -12,8 +12,6 @@ import space.kklochko.spring_rest_example.db.repositories.UserRepository;
import space.kklochko.spring_rest_example.models.Token;
import space.kklochko.spring_rest_example.models.User;
import space.kklochko.spring_rest_example.models.factories.TokenFactory;
import space.kklochko.spring_rest_example.models.rest.RestError;
import space.kklochko.spring_rest_example.models.rest.RestMessage;
import space.kklochko.spring_rest_example.security.hashes.Sha256Hash;
@RestController
@ -29,28 +27,28 @@ public class AuthController {
private TokenFactory tokenFactory;
@PostMapping("/login")
public ResponseEntity<Object> authenticateUser(@RequestBody User user){
public ResponseEntity<String> authenticateUser(@RequestBody User user){
if(user.getUsername() == null || user.getUsername().isEmpty())
return new ResponseEntity<>(new RestError("Failed to login, because no username!!!"), HttpStatus.BAD_REQUEST);
return ResponseEntity.status(500).body("Failed to login, because no username!!!");
if(user.getPassword() == null || user.getPassword().isEmpty())
return new ResponseEntity<>(new RestError("Failed to login, because no password!!!"), HttpStatus.BAD_REQUEST);
return ResponseEntity.status(500).body("Failed to login, because no password!!!");
User expected = userRepository.read(user.getUsername());
if(expected == null)
return new ResponseEntity<>(new RestError("Failed to login, because the user does not exist!!! Please, check your username!!!"), HttpStatus.NOT_FOUND);
return ResponseEntity.status(500).body("Failed to login, because the user does not exist!!! Please, check your username!!!");
String hashedPassword = (new Sha256Hash()).hashOf(user.getPassword());
if(!expected.getPassword().equals(hashedPassword))
return new ResponseEntity<>(new RestError("Failed to login, because incorrect password!!! Please, check your password!!!"), HttpStatus.UNAUTHORIZED);
return ResponseEntity.status(500).body("Failed to login, because incorrect password!!! Please, check your password!!!");
Token token = tokenFactory.create(expected.getUsername(), expected.getRole());
tokenRepository.create(token);
return new ResponseEntity<>(new RestMessage(String.format("token: %s", token.getToken())), HttpStatus.OK);
return new ResponseEntity<>(String.format("token: %s", token.getToken()), HttpStatus.OK);
}
}

@ -7,8 +7,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import space.kklochko.spring_rest_example.db.repositories.DepartmentRepository;
import space.kklochko.spring_rest_example.models.Department;
import space.kklochko.spring_rest_example.models.rest.RestError;
import space.kklochko.spring_rest_example.models.rest.RestMessage;
import java.util.List;
import java.util.UUID;
@ -28,14 +26,14 @@ public class DepartmentController {
Department isExist = departmentRepository.read(department.getId());
if(isExist != null)
return new ResponseEntity<>(new RestError("Failed to create the department, because the id is already used!!!"), HttpStatus.CONFLICT);
return ResponseEntity.status(500).body("Failed to create the department, because the id is already used!!!");
boolean successful = departmentRepository.create(department);
if(!successful)
return new ResponseEntity<>(new RestError("Failed to create the department."), HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseEntity.status(500).body("Failed to create the department");
return new ResponseEntity<>(new RestMessage("The department created successfully."), HttpStatus.CREATED);
return ResponseEntity.status(201).body("The department created successfully");
}
@GetMapping("/{id}")
@ -44,7 +42,7 @@ public class DepartmentController {
Department department = departmentRepository.read(id);
if(department == null)
return new ResponseEntity<>(new RestError("The department not found."), HttpStatus.NOT_FOUND);
return new ResponseEntity<>("Department not found", HttpStatus.NOT_FOUND);
return new ResponseEntity<>(department, HttpStatus.OK);
}
@ -61,14 +59,14 @@ public class DepartmentController {
Department isExist = departmentRepository.read(department.getId());
if(isExist == null)
return new ResponseEntity<>(new RestError("Failed to update the department, because the department does not exist!!!"), HttpStatus.NOT_FOUND);
return ResponseEntity.status(500).body("Failed to update the department, because the department does not exist!!!");
boolean successful = departmentRepository.update(department);
if(!successful)
return new ResponseEntity<>(new RestError("Failed to update the department."), HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseEntity.status(500).body("Failed to update the department.");
return new ResponseEntity<>(new RestMessage("The department updated successfully."), HttpStatus.OK);
return ResponseEntity.status(200).body("The department updated successfully");
}
@DeleteMapping("/{id}")
@ -77,9 +75,8 @@ public class DepartmentController {
boolean successful = departmentRepository.delete(id);
if(!successful)
return new ResponseEntity<>(new RestError("Failed to delete the department, because the department does not exist!!!"), HttpStatus.NOT_FOUND);
return ResponseEntity.status(500).body("Failed to delete the department, because the department does not exist!!!");
return new ResponseEntity<>(new RestMessage("The department deleted successfully."), HttpStatus.OK);
return ResponseEntity.status(201).body("The department deleted successfully");
}
}

@ -7,8 +7,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import space.kklochko.spring_rest_example.db.repositories.IndicatorRepository;
import space.kklochko.spring_rest_example.models.Indicator;
import space.kklochko.spring_rest_example.models.rest.RestError;
import space.kklochko.spring_rest_example.models.rest.RestMessage;
import java.util.List;
import java.util.UUID;
@ -28,14 +26,14 @@ public class IndicatorController {
Indicator isExist = indicatorRepository.read(indicator.getId());
if(isExist != null)
return new ResponseEntity<>(new RestError("Failed to create the indicator, because the id is already used!!!"), HttpStatus.CONFLICT);
return ResponseEntity.status(500).body("Failed to create the indicator, because the id is already used!!!");
boolean successful = indicatorRepository.create(indicator);
if(!successful)
return new ResponseEntity<>(new RestError("Failed to create the indicator."), HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseEntity.status(500).body("Failed to create the indicator");
return new ResponseEntity<>(new RestMessage("The indicator created successfully."), HttpStatus.CREATED);
return ResponseEntity.status(201).body("The indicator created successfully");
}
@GetMapping("/{id}")
@ -44,7 +42,7 @@ public class IndicatorController {
Indicator indicator = indicatorRepository.read(id);
if(indicator == null)
return new ResponseEntity<>(new RestError("The indicator not found."), HttpStatus.NOT_FOUND);
return new ResponseEntity<>("Indicator not found", HttpStatus.NOT_FOUND);
return new ResponseEntity<>(indicator, HttpStatus.OK);
}
@ -61,14 +59,14 @@ public class IndicatorController {
Indicator isExist = indicatorRepository.read(indicator.getId());
if(isExist == null)
return new ResponseEntity<>(new RestError("Failed to update the indicator, because the indicator does not exist!!!"), HttpStatus.NOT_FOUND);
return ResponseEntity.status(500).body("Failed to update the indicator, because the indicator does not exist!!!");
boolean successful = indicatorRepository.update(indicator);
if(!successful)
return new ResponseEntity<>(new RestError("Failed to update the indicator."), HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseEntity.status(500).body("Failed to update the indicator.");
return new ResponseEntity<>(new RestMessage("The indicator updated successfully."), HttpStatus.OK);
return ResponseEntity.status(200).body("The indicator updated successfully");
}
@DeleteMapping("/{id}")
@ -77,9 +75,8 @@ public class IndicatorController {
boolean successful = indicatorRepository.delete(id);
if(!successful)
return new ResponseEntity<>(new RestError("Failed to delete the indicator, because the indicator does not exist!!!"), HttpStatus.NOT_FOUND);
return ResponseEntity.status(500).body("Failed to delete the indicator, because the indicator does not exist!!!");
return new ResponseEntity<>(new RestMessage("The indicator deleted successfully."), HttpStatus.OK);
return ResponseEntity.status(201).body("The indicator deleted successfully");
}
}

@ -7,8 +7,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import space.kklochko.spring_rest_example.db.repositories.PatientRepository;
import space.kklochko.spring_rest_example.models.Patient;
import space.kklochko.spring_rest_example.models.rest.RestError;
import space.kklochko.spring_rest_example.models.rest.RestMessage;
import java.util.List;
import java.util.UUID;
@ -28,14 +26,14 @@ public class PatientController {
Patient isExist = patientRepository.read(patient.getId());
if(isExist != null)
return new ResponseEntity<>(new RestError("Failed to create the patient, because the id is already used!!!"), HttpStatus.CONFLICT);
return ResponseEntity.status(500).body("Failed to create the patient, because the id is already used!!!");
boolean successful = patientRepository.create(patient);
if(!successful)
return new ResponseEntity<>(new RestError("Failed to create the patient."), HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseEntity.status(500).body("Failed to create the patient");
return new ResponseEntity<>(new RestMessage("The patient created successfully."), HttpStatus.CREATED);
return ResponseEntity.status(201).body("The patient created successfully");
}
@GetMapping("/{id}")
@ -44,7 +42,7 @@ public class PatientController {
Patient patient = patientRepository.read(id);
if(patient == null)
return new ResponseEntity<>(new RestError("The patient not found."), HttpStatus.NOT_FOUND);
return new ResponseEntity<>("Patient not found", HttpStatus.NOT_FOUND);
return new ResponseEntity<>(patient, HttpStatus.OK);
}
@ -61,14 +59,14 @@ public class PatientController {
Patient isExist = patientRepository.read(patient.getId());
if(isExist == null)
return new ResponseEntity<>(new RestError("Failed to update the patient, because the patient does not exist!!!"), HttpStatus.NOT_FOUND);
return ResponseEntity.status(500).body("Failed to update the patient, because the patient does not exist!!!");
boolean successful = patientRepository.update(patient);
if(!successful)
return new ResponseEntity<>(new RestError("Failed to update the patient."), HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseEntity.status(500).body("Failed to update the patient.");
return new ResponseEntity<>(new RestMessage("The patient updated successfully."), HttpStatus.OK);
return ResponseEntity.status(200).body("The patient updated successfully");
}
@DeleteMapping("/{id}")
@ -77,9 +75,8 @@ public class PatientController {
boolean successful = patientRepository.delete(id);
if(!successful)
return new ResponseEntity<>(new RestError("Failed to delete the patient, because the patient does not exist!!!"), HttpStatus.NOT_FOUND);
return ResponseEntity.status(500).body("Failed to delete the patient, because the patient does not exist!!!");
return new ResponseEntity<>(new RestMessage("The patient deleted successfully."), HttpStatus.OK);
return ResponseEntity.status(201).body("The patient deleted successfully");
}
}

@ -1,15 +0,0 @@
package space.kklochko.spring_rest_example.models.rest;
import lombok.AllArgsConstructor;
import lombok.Data;
@AllArgsConstructor
@Data
public class RestError {
public String error;
public RestError() {
error = "";
}
}

@ -1,15 +0,0 @@
package space.kklochko.spring_rest_example.models.rest;
import lombok.AllArgsConstructor;
import lombok.Data;
@AllArgsConstructor
@Data
public class RestMessage {
public String message;
public RestMessage() {
message = "";
}
}
Loading…
Cancel
Save