Commit 2f0b01c7 authored by oleg.pahhomov's avatar oleg.pahhomov

SA0140-568 | add user news property

parent efca98c7
......@@ -53,7 +53,8 @@ public class MyUserDetailsService implements UserDetailsService {
.map(AppUserRole::getRole)
.collect(Collectors.toList());
roleStrings.add(AppRoles.USER);
return new StatUser(user.getId(), user.getFullName(), user.getIdCode(), encoder.encode(token), getAuthorities(roleStrings));
return new StatUser(user.getId(), user.getFullName(), user.isSeenTheNews(),
user.getIdCode(), encoder.encode(token), getAuthorities(roleStrings));
}
private static List<GrantedAuthority> getAuthorities(List<AppRoles> roles) {
......@@ -62,4 +63,4 @@ public class MyUserDetailsService implements UserDetailsService {
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
}
}
\ No newline at end of file
}
......@@ -9,17 +9,18 @@ public class StatUser extends User {
private Long id;
private String fullName;
private boolean seenTheNews;
public StatUser(Long id, String fullName, String username, String password, Collection<? extends GrantedAuthority> authorities) {
public StatUser(Long id, String fullName, boolean seenTheNews, String username, String password, Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
this.id = id;
this.fullName = fullName;
this.seenTheNews = seenTheNews;
}
public StatUser(Long id, String fullName, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
public StatUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
this.id = id;
this.fullName = fullName;
throw new UnsupportedOperationException("don't use this constructor");
}
public String getFullName() {
......@@ -29,4 +30,8 @@ public class StatUser extends User {
public Long getId() {
return id;
}
public boolean getSeenTheNews() {
return seenTheNews;
}
}
......@@ -4,9 +4,14 @@ import ee.stat.dashboard.config.security.RoleString;
import ee.stat.dashboard.config.security.StatUser;
import ee.stat.dashboard.controller.Response;
import ee.stat.dashboard.controller.user.dto.UserData;
import ee.stat.dashboard.service.user.UserService;
import ee.stat.dashboard.service.user.dto.UserNewsDto;
import lombok.AllArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -14,15 +19,29 @@ import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import static ee.stat.dashboard.controller.Response.of;
@RestController
@RequestMapping("/me")
@AllArgsConstructor
public class MeController {
private UserService userService;
@GetMapping
@Secured(RoleString.USER)
public Response<UserData> userData() {
StatUser user = UserSessionHolder.getLoggedInUser();
return user == null ? null : Response.of(new UserData(user.getFullName(), getRoles(user.getAuthorities())));
return user == null ? null : of(new UserData(user.getFullName(), getRoles(user.getAuthorities()), user.getSeenTheNews() ? true : null));
}
@PatchMapping(path = "/news")
@Secured(RoleString.USER)
public Response<UserNewsDto> userData(@RequestBody UserNewsDto userNewsDto) {
StatUser user = UserSessionHolder.getLoggedInUserOrThrow("user");
userNewsDto.setId(user.getId());
userService.seenTheNews(userNewsDto);
return of(userNewsDto);
}
private List<String> getRoles(Collection<GrantedAuthority> authorities) {
......
......@@ -14,4 +14,5 @@ import java.util.List;
public class UserData {
private String username;
private List<String> roles;
}
\ No newline at end of file
private Boolean seenTheNews;
}
......@@ -20,6 +20,7 @@ public class AppUser {
private String idCode;
private String firstName;
private String lastName;
private boolean seenTheNews;
@Transient
private AppUserSession session;
......
......@@ -4,6 +4,7 @@ import ee.stat.dashboard.controller.user.dto.TaraDataDto;
import ee.stat.dashboard.model.user.user.AppUser;
import ee.stat.dashboard.model.user.user.AppUserSession;
import ee.stat.dashboard.repository.UserRepository;
import ee.stat.dashboard.service.user.dto.UserNewsDto;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -33,6 +34,12 @@ public class UserService {
return user;
}
public void seenTheNews(UserNewsDto userNewsDto) {
AppUser user = userRepository.findById(userNewsDto.getId()).orElseThrow(RuntimeException::new);
user.setSeenTheNews(true);
userRepository.save(user);
}
private AppUser getOrSaveNewUser(TaraDataDto dto) {
AppUser existingUser = userRepository.findByIdCode(dto.getIdCode());
if (existingUser != null) {
......
package ee.stat.dashboard.service.user.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class UserNewsDto {
private Long id;
private Boolean seenNews;
}
ALTER TABLE app_user
ADD seen_the_news BOOLEAN NOT NULL DEFAULT FALSE;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment