Skip to content

Commit

Permalink
Merge branch 'feature/action' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
kolya-t committed Jun 20, 2017
2 parents 6077348 + 962d42b commit 28d6dbf
Show file tree
Hide file tree
Showing 19 changed files with 383 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
import org.springframework.context.annotation.Profile;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.teapot.backend.model.TeapotProperty;
import org.teapot.backend.model.meta.TeapotAction;
import org.teapot.backend.model.meta.TeapotProperty;
import org.teapot.backend.model.User;
import org.teapot.backend.model.UserAuthority;
import org.teapot.backend.model.meta.TeapotResource;
import org.teapot.backend.repository.TeapotActionRepository;
import org.teapot.backend.repository.TeapotPropertyRepository;
import org.teapot.backend.repository.TeapotResourceRepository;
import org.teapot.backend.repository.UserRepository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;


@Configuration
Expand All @@ -29,6 +34,12 @@ public class DevelopmentProfileConfig {
@Autowired
private TeapotPropertyRepository propertyRepository;

@Autowired
private TeapotActionRepository actionRepository;

@Autowired
private TeapotResourceRepository resourceRepository;

@Autowired
private PasswordEncoder passwordEncoder;

Expand Down Expand Up @@ -57,6 +68,8 @@ public CommandLineRunner loadData() {
registerDoctorWatson();

addProperties();
addResources();
addActions();
};
}

Expand Down Expand Up @@ -158,4 +171,29 @@ private void addProperties() {
propertyRepository.save(property1);
propertyRepository.save(property2);
}

private void addResources() {
TeapotResource resource1 = new TeapotResource();

resource1.setName("user");
resource1.setUri("/users");
resource1.setDescription(
"Available methods:\n\nGET /users/{id|username}\n" +
"POST /users\nPUT /users\nDELETE /users/{id}\n\n");

resourceRepository.save(resource1);
}

private void addActions() {
TeapotAction action1 = new TeapotAction();
TeapotAction action2 = new TeapotAction();

action1.setName("help");
action1.setUsage("/actions/help?resource={name|id}|action={name|id}");

action2.setName("activate");
action2.setUsage("/actions/activate?user={username|id}&token={token}");

actionRepository.save(Arrays.asList(action1, action2));
}
}

This file was deleted.

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());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.teapot.backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
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.TeapotProperty;
import org.teapot.backend.model.meta.TeapotProperty;
import org.teapot.backend.repository.TeapotPropertyRepository;

import javax.servlet.http.HttpServletResponse;
Expand Down
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);
}
}
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 {
}
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 {
}
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 {
}
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 {
}
11 changes: 0 additions & 11 deletions src/main/java/org/teapot/backend/model/VerificationToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public class VerificationToken {
@Column(nullable = false)
private LocalDateTime expireDateTime = LocalDateTime.now().plusDays(1);

@Column(name = "is_activated")
private Boolean isActivated = false;

@OneToOne(fetch = FetchType.LAZY)
private User user;

Expand All @@ -38,10 +35,6 @@ public LocalDateTime getExpireDateTime() {
return expireDateTime;
}

public Boolean isActivated() {
return isActivated;
}

public User getUser() {
return user;
}
Expand All @@ -58,10 +51,6 @@ public void setExpireDateTime(LocalDateTime expireDateTime) {
this.expireDateTime = expireDateTime;
}

public void setActivated(Boolean activated) {
isActivated = activated;
}

public void setUser(User user) {
this.user = user;
}
Expand Down
Loading

0 comments on commit 28d6dbf

Please sign in to comment.