Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update User & skills schema, create endpoint to create a skill and get all skills & remove unit and integration tests #131

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.RDS.skilltree.Authentication;

import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.User.UserRole;
import com.RDS.skilltree.User.JwtUserModel;
import com.RDS.skilltree.User.UserRoleEnum;
import java.util.List;
import javax.security.auth.Subject;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

public class UserAuthenticationToken extends AbstractAuthenticationToken {

private final UserModel user;
private final JwtUserModel user;

public UserAuthenticationToken(String role, String rdsUserId) {
super(List.of(new SimpleGrantedAuthority(UserRole.fromString(role).name())));
this.user = UserModel.builder().rdsUserId(rdsUserId).role(UserRole.fromString(role)).build();
super(List.of(new SimpleGrantedAuthority(UserRoleEnum.fromString(role).name())));

this.user = new JwtUserModel(rdsUserId, UserRoleEnum.fromString(role));
setAuthenticated(true);
}

Expand All @@ -23,7 +24,7 @@ public Object getCredentials() {
}

@Override
public UserModel getPrincipal() {
public JwtUserModel getPrincipal() {
return user;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.RDS.skilltree.Authentication.AuthEntryPoint;
import com.RDS.skilltree.Authentication.CustomAccessDeniedHandler;
import com.RDS.skilltree.Filters.JWTAuthenticationFilter;
import com.RDS.skilltree.User.UserRole;
import com.RDS.skilltree.User.UserRoleEnum;
import java.util.Arrays;
import java.util.List;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -50,10 +50,10 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
&& request.getQueryString().contains("dummyData=true"))
.permitAll()
.requestMatchers(HttpMethod.GET, "/v1/**")
.hasAnyAuthority(UserRole.getAllRoles()) // give read-only access to all
.hasAnyAuthority(UserRoleEnum.getAllRoles()) // give read-only access to all
.requestMatchers("/v1/**")
.hasAnyAuthority(
UserRole.USER.name(), UserRole.MEMBER.name(), UserRole.SUPERUSER.name())
UserRoleEnum.USER.name(), UserRoleEnum.MEMBER.name(), UserRoleEnum.SUPERUSER.name())
.anyRequest()
.authenticated())
.exceptionHandling(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.RDS.skilltree.Endorsement;

import jakarta.validation.constraints.NotNull;

import java.util.UUID;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -16,5 +18,5 @@ public class EndorsementDRO {
private UUID endorseeId;

@NotNull(message = "skill id cannot be null")
private UUID skillId;
private Integer skillId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public static EndorsementDTO toDto(EndorsementModel endorsementModel) {
.build();
endorsementDTO.setCreatedAt(endorsementModel.getCreatedAt());
endorsementDTO.setUpdatedAt(endorsementModel.getUpdatedAt());
endorsementDTO.setCreatedBy(endorsementModel.getCreatedBy());
endorsementDTO.setUpdatedBy(endorsementModel.getUpdatedBy());
return endorsementDTO;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.RDS.skilltree.Endorsement;

import com.RDS.skilltree.EndorsementList.EndorsementListModel;
import com.RDS.skilltree.Skill.SkillModel;
import com.RDS.skilltree.Skill.Skill;
import com.RDS.skilltree.utils.TrackedProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
Expand All @@ -25,9 +25,9 @@ public class EndorsementModel extends TrackedProperties {
@Column(name = "endorsee_id")
private UUID endorseeId;

@ManyToOne(targetEntity = SkillModel.class, cascade = CascadeType.ALL)
@ManyToOne(targetEntity = Skill.class, cascade = CascadeType.ALL)
@JoinColumn(name = "skill_id", referencedColumnName = "id")
private SkillModel skill;
private Skill skill;

@OneToMany(mappedBy = "endorsement")
@JsonManagedReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
public interface EndorsementRepository extends JpaRepository<EndorsementModel, UUID> {
List<EndorsementModel> findByEndorseeId(UUID userId);

List<EndorsementModel> findBySkillId(UUID skillId);
List<EndorsementModel> findBySkillId(Integer skillId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
import com.RDS.skilltree.Exceptions.EntityAlreadyExistsException;
import com.RDS.skilltree.Exceptions.InvalidParameterException;
import com.RDS.skilltree.Exceptions.NoEntityException;
import com.RDS.skilltree.Skill.SkillModel;
import com.RDS.skilltree.Skill.Skill;
import com.RDS.skilltree.Skill.SkillRepository;
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.User.UserRole;
import com.RDS.skilltree.User.JwtUserModel;
import com.RDS.skilltree.User.UserRoleEnum;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityNotFoundException;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -78,7 +80,8 @@ public Page<EndorsementModelFromJSON> getEndorsementsFromDummyData(
private List<EndorsementModelFromJSON> readEndorsementsFromJSON() throws IOException {
ClassPathResource resource = new ClassPathResource(dummyEndorsementDataPath);
return objectMapper.readValue(
resource.getInputStream(), new TypeReference<List<EndorsementModelFromJSON>>() {});
resource.getInputStream(), new TypeReference<List<EndorsementModelFromJSON>>() {
});
}

private List<EndorsementModelFromJSON> filterEndorsements(
Expand Down Expand Up @@ -118,9 +121,9 @@ private Page<EndorsementModelFromJSON> createPagedEndorsements(
@Override
public EndorsementModel createEndorsement(EndorsementDRO endorsementDRO) {
UUID userId = endorsementDRO.getEndorseeId();
UUID skillId = endorsementDRO.getSkillId();
Integer skillId = endorsementDRO.getSkillId();

Optional<SkillModel> skillOptional = skillRepository.findById(skillId);
Optional<Skill> skillOptional = skillRepository.findById(skillId);
if (skillOptional.isPresent()) {
EndorsementModel endorsementModel =
EndorsementModel.builder().endorseeId(userId).skill(skillOptional.get()).build();
Expand All @@ -134,9 +137,9 @@ public EndorsementModel createEndorsement(EndorsementDRO endorsementDRO) {

@Override
public GenericResponse<Void> updateEndorsementStatus(UUID id, String status) {
UserModel user =
(UserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (!user.getRole().equals(UserRole.SUPERUSER)) {
JwtUserModel user =
(JwtUserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (!user.getRole().equals(UserRoleEnum.SUPERUSER)) {
throw new AccessDeniedException("Unauthorized, Access is only available to super users");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package com.RDS.skilltree.Skill;

import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.utils.TrackedProperties;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import java.util.Set;
import java.util.UUID;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.UUID;

@AllArgsConstructor
@NoArgsConstructor
@Entity
@Builder
@Getter
@Table(name = "Skill")
public class SkillModel extends TrackedProperties {
@Table(name = "skills")
public class Skill extends TrackedProperties {
@Id
@GeneratedValue
@Column(name = "id", columnDefinition = "BINARY(16)")
private UUID id;
@Column(name = "id")
private Integer id;

@Column(name = "name", unique = true, nullable = false)
private String name;

@Column(name = "skill_type", nullable = false)
@Enumerated(value = EnumType.STRING)
private SkillType type = SkillType.ATOMIC;
private SkillTypeEnum type = SkillTypeEnum.ATOMIC;

@Column(name = "is_deleted", nullable = false)
private boolean deleted;
private boolean isDeleted;

// TODO : Confirm the type of this column from tejas
@Column(name = "created_by", nullable = false)
private UUID createdBy;

@JsonBackReference
@JsonIgnore
@ManyToMany(mappedBy = "skills", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserModel> users;
@Column(name = "updated_by")
private UUID updatedBy;
}
15 changes: 2 additions & 13 deletions skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.validation.constraints.NotNull;
import java.util.UUID;

import lombok.Builder;
import lombok.Getter;

Expand All @@ -14,16 +14,5 @@ public class SkillDRO {
private String name;

@NotNull(message = "SkillType cannot be null")
private SkillType type;

@NotNull(message = "Created by user Id cannot be null")
private UUID createdBy;

public static SkillModel toModel(SkillDRO skillDRO) {
return SkillModel.builder()
.name(skillDRO.getName())
.type(skillDRO.getType())
.deleted(false)
.build();
}
private SkillTypeEnum type;
}
28 changes: 13 additions & 15 deletions skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,37 @@

import com.RDS.skilltree.User.UserDTO;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class SkillDTO {
private UUID id;
private SkillType type;
private Integer id;
private SkillTypeEnum type;
private String name;
private Set<UserDTO> users;

public static SkillDTO toDto(SkillModel skillModel) {
public static SkillDTO toDto(Skill skill) {
return SkillDTO.builder()
.id(skillModel.getId())
.name(skillModel.getName())
.type(skillModel.getType())
.id(skill.getId())
.name(skill.getName())
.type(skill.getType())
.build();
}

public static SkillDTO getSkillsWithUsers(SkillModel skillModel) {
public static SkillDTO getSkillsWithUsers(Skill skill) {
Set<UserDTO> users = new HashSet<>();
if (skillModel.getUsers() != null) {
users = skillModel.getUsers().stream().map(UserDTO::toDTO).collect(Collectors.toSet());
}

return SkillDTO.builder()
.id(skillModel.getId())
.name(skillModel.getName())
.type(skillModel.getType())
.id(skill.getId())
.name(skill.getName())
.type(skill.getType())
.users(users)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import java.util.Optional;
import java.util.UUID;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SkillRepository extends JpaRepository<SkillModel, UUID> {
Optional<SkillModel> findByName(String name);

Page<SkillModel> findAll(Pageable pageable);
public interface SkillRepository extends JpaRepository<Skill, Integer> {
Optional<Skill> findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.RDS.skilltree.Skill;

public enum SkillType {
public enum SkillTypeEnum {
ATOMIC,
DERIVED
}
Loading
Loading