-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/action' into dev
- Loading branch information
Showing
19 changed files
with
383 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
src/main/java/org/teapot/backend/controller/RestResponseEntityExceptionHandler.java
This file was deleted.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
src/main/java/org/teapot/backend/controller/TeapotActionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.teapot.backend.controller; | ||
|
||
import com.google.common.primitives.Longs; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.teapot.backend.controller.exception.BadRequestException; | ||
import org.teapot.backend.controller.exception.ConflictException; | ||
import org.teapot.backend.controller.exception.ResourceNotFoundException; | ||
import org.teapot.backend.model.User; | ||
import org.teapot.backend.model.VerificationToken; | ||
import org.teapot.backend.model.meta.TeapotAction; | ||
import org.teapot.backend.repository.TeapotActionRepository; | ||
import org.teapot.backend.repository.TeapotResourceRepository; | ||
import org.teapot.backend.repository.UserRepository; | ||
import org.teapot.backend.repository.VerificationTokenRepository; | ||
import org.teapot.backend.util.VerificationMailSender; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
@RestController | ||
@RequestMapping("/actions") | ||
public class TeapotActionController { | ||
|
||
@Autowired | ||
private TeapotActionRepository actionRepository; | ||
|
||
@Autowired | ||
private TeapotResourceRepository resourceRepository; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private VerificationTokenRepository tokenRepository; | ||
|
||
@Autowired | ||
private VerificationMailSender verificationMailSender; | ||
|
||
@GetMapping("/{nameOrId}") | ||
public TeapotAction getAction(@PathVariable String nameOrId) { | ||
Long id = Longs.tryParse(nameOrId); | ||
return Optional.ofNullable((id != null) | ||
? actionRepository.findOne(id) | ||
: actionRepository.findByName(nameOrId)) | ||
.orElseThrow(ResourceNotFoundException::new); | ||
} | ||
|
||
@GetMapping("/help") | ||
public Object help( | ||
@RequestParam(name = "resource", required = false) String resourceNameOrId, | ||
@RequestParam(name = "action", required = false) String actionNameOrId | ||
) { | ||
if ((resourceNameOrId != null) && (actionNameOrId != null)) { | ||
throw new ConflictException(); | ||
} | ||
|
||
if (resourceNameOrId != null) { | ||
Long id = Longs.tryParse(resourceNameOrId); | ||
return Optional.ofNullable((id != null) | ||
? resourceRepository.findOne(id) | ||
: resourceRepository.findByName(resourceNameOrId)) | ||
.orElseThrow(ResourceNotFoundException::new); | ||
} | ||
|
||
if (actionNameOrId != null) { | ||
Long id = Longs.tryParse(actionNameOrId); | ||
return Optional.ofNullable((id != null) | ||
? actionRepository.findOne(id) | ||
: actionRepository.findByName(actionNameOrId)) | ||
.orElseThrow(ResourceNotFoundException::new); | ||
} | ||
|
||
throw new BadRequestException(); | ||
} | ||
|
||
@PostMapping("/activate") | ||
public void activate( | ||
@RequestParam("token") String tokenString, | ||
WebRequest request | ||
) { | ||
VerificationToken token = Optional | ||
.ofNullable(tokenRepository.findByToken(tokenString)) | ||
.orElseThrow(ResourceNotFoundException::new); | ||
User user = token.getUser(); | ||
|
||
tokenRepository.delete(token); | ||
|
||
if (token.getExpireDateTime().isAfter(LocalDateTime.now())) { | ||
user.setActivated(true); | ||
userRepository.save(user); | ||
} else { | ||
verificationMailSender.createTokenAndSend(user, request.getLocale()); | ||
} | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
src/main/java/org/teapot/backend/controller/TeapotPropertyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/org/teapot/backend/controller/TeapotResourceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.teapot.backend.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.teapot.backend.controller.exception.BadRequestException; | ||
import org.teapot.backend.controller.exception.ResourceNotFoundException; | ||
import org.teapot.backend.model.meta.TeapotResource; | ||
import org.teapot.backend.repository.TeapotResourceRepository; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/resources") | ||
public class TeapotResourceController { | ||
|
||
@Autowired | ||
private TeapotResourceRepository resourceRepository; | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.CREATED) | ||
private TeapotResource addResource(@RequestBody TeapotResource resource, | ||
HttpServletResponse response) { | ||
|
||
if (resourceRepository.findByName(resource.getName()) != null) { | ||
throw new BadRequestException(); | ||
} | ||
|
||
resource = resourceRepository.save(resource); | ||
response.setHeader("Location", "/resources/" + resource.getId()); | ||
|
||
return resource; | ||
} | ||
|
||
@GetMapping("/{resourceNameOrId}") | ||
public TeapotResource getResource(@PathVariable String resourceNameOrId) { | ||
TeapotResource resource; | ||
|
||
Long id = Long.valueOf(resourceNameOrId); | ||
|
||
if (id != null) { | ||
resource = resourceRepository.findOne(id); | ||
} else { | ||
resource = resourceRepository.findByName(resourceNameOrId); | ||
} | ||
|
||
if (resource == null) { | ||
throw new ResourceNotFoundException(); | ||
} | ||
|
||
return resource; | ||
} | ||
|
||
@GetMapping | ||
public List<TeapotResource> getResources(Pageable pageable) { | ||
return resourceRepository.findAll(pageable).getContent(); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void updateResource(@PathVariable Long id, | ||
@RequestBody TeapotResource resource) { | ||
|
||
TeapotResource original = resourceRepository.findOne(id); | ||
|
||
if (original == null) { | ||
throw new ResourceNotFoundException(); | ||
} | ||
|
||
resource.setId(original.getId()); | ||
|
||
resourceRepository.save(resource); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void deleteResource(@PathVariable Long id) { | ||
if (!resourceRepository.exists(id)) { | ||
throw new ResourceNotFoundException(); | ||
} | ||
|
||
resourceRepository.delete(id); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/org/teapot/backend/controller/exception/BadRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
package org.teapot.backend.controller.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public class BadRequestException extends RuntimeException { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/org/teapot/backend/controller/exception/ConflictException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.teapot.backend.controller.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.CONFLICT) | ||
public class ConflictException extends RuntimeException { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/org/teapot/backend/controller/exception/ForbiddenException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
package org.teapot.backend.controller.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.FORBIDDEN) | ||
public class ForbiddenException extends RuntimeException { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/org/teapot/backend/controller/exception/ResourceNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
package org.teapot.backend.controller.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
public class ResourceNotFoundException extends RuntimeException { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.