Merge branch '49-create-accounts-rest-controller' into 'master'

Resolve "Create accounts rest controller"

Closes #49

See merge request marcel.schwarz/2020ss-qbc-geofence-timetracking!47
This commit is contained in:
Marcel Schwarz 2020-05-13 13:01:57 +00:00
commit d47733cc6f
16 changed files with 66 additions and 28 deletions

View File

@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@SpringBootApplication
@ -19,4 +20,9 @@ public class GeotimeApplication {
return new BCryptPasswordEncoder();
}
@Bean
public SpelAwareProxyProjectionFactory projectionFactory() {
return new SpelAwareProxyProjectionFactory();
}
}

View File

@ -1,5 +1,9 @@
package de.hft.geotime.user;
package de.hft.geotime.controllers;
import de.hft.geotime.entities.TimetrackUser;
import de.hft.geotime.entities.projections.UserWithRoleProjection;
import de.hft.geotime.repositories.TimetrackUserRepository;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
@ -14,16 +18,18 @@ public class UserController {
private final TimetrackUserRepository userRepository;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final ProjectionFactory projectionFactory;
public UserController(TimetrackUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder) {
public UserController(TimetrackUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder, ProjectionFactory projectionFactory) {
this.userRepository = userRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
this.projectionFactory = projectionFactory;
}
@GetMapping("/whoami")
public String getUsername(Authentication authentication) {
TimetrackUser timetrackUser = userRepository.findFirstByUsername(authentication.getName());
return "Welcome back " + timetrackUser.getFirstname() + " " + timetrackUser.getLastname();
public UserWithRoleProjection getUsername(Authentication authentication) {
TimetrackUser user = userRepository.findFirstByUsername(authentication.getName());
return projectionFactory.createProjection(UserWithRoleProjection.class, user);
}
@PostMapping("/sign-up")

View File

@ -1,4 +1,4 @@
package de.hft.geotime.record;
package de.hft.geotime.entities;
public enum RecordType {

View File

@ -1,4 +1,4 @@
package de.hft.geotime.role;
package de.hft.geotime.entities;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,6 +1,5 @@
package de.hft.geotime.record;
package de.hft.geotime.entities;
import de.hft.geotime.timetrackaccount.TimetrackAccount;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

View File

@ -1,6 +1,5 @@
package de.hft.geotime.timetrackaccount;
package de.hft.geotime.entities;
import de.hft.geotime.user.TimetrackUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

View File

@ -1,8 +1,6 @@
package de.hft.geotime.user;
package de.hft.geotime.entities;
import com.fasterxml.jackson.annotation.JsonIgnore;
import de.hft.geotime.role.Role;
import de.hft.geotime.timetrackaccount.TimetrackAccount;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

View File

@ -0,0 +1,18 @@
package de.hft.geotime.entities.projections;
import de.hft.geotime.entities.TimetrackAccount;
import de.hft.geotime.entities.TimetrackUser;
import org.springframework.data.rest.core.config.Projection;
@Projection(name = "withUser", types = TimetrackAccount.class)
public interface AccountWithUserProjection {
double getRevenue();
String getName();
String getDescription();
TimetrackUser getUser();
}

View File

@ -1,10 +1,11 @@
package de.hft.geotime.user;
package de.hft.geotime.entities.projections;
import de.hft.geotime.entities.TimetrackUser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.core.config.Projection;
@Projection(name = "withRole", types = TimetrackUser.class)
public interface BasicUserProjection {
public interface UserWithRoleProjection {
String getFirstname();

View File

@ -1,5 +1,6 @@
package de.hft.geotime.record;
package de.hft.geotime.repositories;
import de.hft.geotime.entities.TimeRecord;
import org.springframework.data.repository.CrudRepository;
public interface RecordRepository extends CrudRepository<TimeRecord, Long> {

View File

@ -1,5 +1,6 @@
package de.hft.geotime.role;
package de.hft.geotime.repositories;
import de.hft.geotime.entities.Role;
import org.springframework.data.repository.CrudRepository;
public interface RoleRepository extends CrudRepository<Role, Long> {

View File

@ -0,0 +1,14 @@
package de.hft.geotime.repositories;
import de.hft.geotime.entities.TimetrackAccount;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(
path = "accounts",
itemResourceRel = "accounts",
collectionResourceRel = "accounts"
)
public interface TimetrackAccountRepository extends PagingAndSortingRepository<TimetrackAccount, Long> {
}

View File

@ -1,5 +1,6 @@
package de.hft.geotime.user;
package de.hft.geotime.repositories;
import de.hft.geotime.entities.TimetrackUser;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;

View File

@ -1,5 +1,7 @@
package de.hft.geotime.user;
package de.hft.geotime.security;
import de.hft.geotime.entities.TimetrackUser;
import de.hft.geotime.repositories.TimetrackUserRepository;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

View File

@ -1,6 +1,5 @@
package de.hft.geotime.security;
import de.hft.geotime.user.UserDetailsServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

View File

@ -1,7 +0,0 @@
package de.hft.geotime.timetrackaccount;
import org.springframework.data.repository.CrudRepository;
public interface TimetrackAccountRepository extends CrudRepository<TimetrackAccount, Long> {
}