Skip to content

Instantly share code, notes, and snippets.

@OleksandrDanylchenko
Created April 22, 2020 19:33
Show Gist options
  • Save OleksandrDanylchenko/f65118bb78382084af9c0636e3702ae1 to your computer and use it in GitHub Desktop.
Save OleksandrDanylchenko/f65118bb78382084af9c0636e3702ae1 to your computer and use it in GitHub Desktop.
Duplicated resouces and services
package ua.alexd.CarRentService.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ua.alexd.CarRentService.domain.ModelClass;
import ua.alexd.CarRentService.service.ModelClassService;
import javax.validation.Valid;
import java.util.List;
@CrossOrigin(origins = {"http://localhost:3000", "http://localhost:4200", "http://localhost:8081"})
@RestController
@RequestMapping("/class")
public class ClassResource {
private final ModelClassService modelClassService;
public ClassResource(ModelClassService modelClassService) {
this.modelClassService = modelClassService;
}
@GetMapping
public ResponseEntity<List<ModelClass>> getClasses() {
var classes = modelClassService.getAllClasses();
return classes.isEmpty()
? new ResponseEntity<>(HttpStatus.NOT_FOUND)
: new ResponseEntity<>(classes, HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<ModelClass> getClassById(@PathVariable String id) {
var foundedClass = modelClassService.getClassById(id);
return foundedClass
.map(type -> new ResponseEntity<>(type, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@PostMapping
public ResponseEntity<ModelClass> addClass(@Valid @RequestBody ModelClass newClass) {
return modelClassService.addNewClass(newClass)
? new ResponseEntity<>(newClass, HttpStatus.CREATED)
: new ResponseEntity<>(HttpStatus.CONFLICT);
}
@PutMapping
public ResponseEntity<ModelClass> updateClass(@Valid @RequestBody ModelClass updClass) {
return modelClassService.updateClass(updClass)
? new ResponseEntity<>(updClass, HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.CONFLICT);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteClass(@PathVariable String id) {
return modelClassService.deleteClass(id)
? new ResponseEntity<>(HttpStatus.NO_CONTENT)
: new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
package ua.alexd.CarRentService.service;
import org.apache.commons.lang3.math.NumberUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import ua.alexd.CarRentService.domain.ModelClass;
import ua.alexd.CarRentService.repository.ModelClassRepository;
import java.util.List;
import java.util.Optional;
@Service
public class ModelClassService {
private final ModelClassRepository modelClassRepository;
public ModelClassService(ModelClassRepository modelClassRepository) {
this.modelClassRepository = modelClassRepository;
}
public List<ModelClass> getAllClasses() {
return modelClassRepository.findAll();
}
public Optional<ModelClass> getClassById(String id) {
try {
var longId = NumberUtils.createLong(id);
return modelClassRepository.findById(longId);
} catch (NumberFormatException e) {
return Optional.empty();
}
}
public boolean addNewClass(ModelClass newClass) {
return saveClass(newClass);
}
public boolean updateClass(@NotNull ModelClass updClass) {
var classFromDB = getClassById(String.valueOf(updClass.getId()));
if (classFromDB.isEmpty())
return false;
BeanUtils.copyProperties(updClass, classFromDB.get(), "id");
return saveClass(classFromDB.get());
}
private boolean saveClass(ModelClass saveClass) {
try {
modelClassRepository.save(saveClass);
} catch (IllegalArgumentException | DataIntegrityViolationException ignored) {
return false;
}
return true;
}
public boolean deleteClass(String id) {
var deletionClass = getClassById(id);
if (deletionClass.isEmpty())
return false;
modelClassRepository.delete(deletionClass.get());
return true;
}
}
package ua.alexd.CarRentService.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ua.alexd.CarRentService.domain.Type;
import ua.alexd.CarRentService.service.TypeService;
import javax.validation.Valid;
import java.util.List;
@CrossOrigin(origins = {"http://localhost:3000", "http://localhost:4200", "http://localhost:8081"})
@RestController
@RequestMapping("/type")
public class TypeResource {
private final TypeService typeService;
public TypeResource(TypeService typeService) {
this.typeService = typeService;
}
@GetMapping
public ResponseEntity<List<Type>> getModels() {
var types = typeService.getAllTypes();
return types.isEmpty()
? new ResponseEntity<>(HttpStatus.NOT_FOUND)
: new ResponseEntity<>(types, HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<Type> getTypeById(@PathVariable String id) {
var foundedType = typeService.getTypeById(id);
return foundedType
.map(type -> new ResponseEntity<>(type, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@PostMapping
public ResponseEntity<Type> addType(@Valid @RequestBody Type newType) {
return typeService.addNewType(newType)
? new ResponseEntity<>(newType, HttpStatus.CREATED)
: new ResponseEntity<>(HttpStatus.CONFLICT);
}
@PutMapping
public ResponseEntity<Type> updateType(@Valid @RequestBody Type updType) {
return typeService.updateType(updType)
? new ResponseEntity<>(updType, HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.CONFLICT);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteModel(@PathVariable String id) {
return typeService.deleteType(id)
? new ResponseEntity<>(HttpStatus.NO_CONTENT)
: new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
package ua.alexd.CarRentService.service;
import org.apache.commons.lang3.math.NumberUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import ua.alexd.CarRentService.domain.Type;
import ua.alexd.CarRentService.repository.TypeRepository;
import java.util.List;
import java.util.Optional;
@Service
public class TypeService {
private final TypeRepository typeRepository;
public TypeService(TypeRepository typeRepository) {
this.typeRepository = typeRepository;
}
public List<Type> getAllTypes() {
return typeRepository.findAll();
}
public Optional<Type> getTypeById(String id) {
try {
var longId = NumberUtils.createLong(id);
return typeRepository.findById(longId);
} catch (NumberFormatException e) {
return Optional.empty();
}
}
public boolean addNewType(Type newType) {
return saveType(newType);
}
public boolean updateType(@NotNull Type updType) {
var typeFromDB = getTypeById(String.valueOf(updType.getId()));
if (typeFromDB.isEmpty())
return false;
BeanUtils.copyProperties(updType, typeFromDB.get(), "id");
return saveType(typeFromDB.get());
}
private boolean saveType(Type saveType) {
try {
typeRepository.save(saveType);
} catch (IllegalArgumentException | DataIntegrityViolationException ignored) {
return false;
}
return true;
}
public boolean deleteType(String id) {
var deletionModel = getTypeById(id);
if (deletionModel.isEmpty())
return false;
typeRepository.delete(deletionModel.get());
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment