Skip to content

Commit

Permalink
Merge pull request #281 from ita-social-projects/create-achievements
Browse files Browse the repository at this point in the history
Create achievements
  • Loading branch information
ospodaryk authored Oct 4, 2023
2 parents 0e4e203 + 6d408b6 commit 62683a0
Show file tree
Hide file tree
Showing 18 changed files with 27 additions and 165 deletions.
12 changes: 8 additions & 4 deletions dao/src/main/java/greencity/entity/Achievement.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package greencity.entity;

import greencity.entity.localization.AchievementTranslation;
import lombok.*;

import javax.persistence.*;
Expand All @@ -19,8 +18,13 @@ public class Achievement {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(cascade = {CascadeType.ALL}, mappedBy = "achievement", fetch = FetchType.LAZY)
private List<AchievementTranslation> translations;
@Column(nullable = false, length = 300)
private String title;

@Column(nullable = false, length = 300)
private String name;
@Column(nullable = false, length = 300)
private String nameEng;

@OneToMany(cascade = {CascadeType.ALL}, mappedBy = "achievement", fetch = FetchType.LAZY)
private List<UserAchievement> userAchievements;
Expand All @@ -30,4 +34,4 @@ public class Achievement {

@Column(nullable = false)
private Integer condition;
}
}
4 changes: 0 additions & 4 deletions dao/src/main/java/greencity/entity/AchievementCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public class AchievementCategory {

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

@OneToMany(mappedBy = "achievementCategory")
private List<Achievement> achievementList;

@OneToMany(mappedBy = "achievementCategory")
private List<UserAction> userActions;
}
7 changes: 1 addition & 6 deletions dao/src/main/java/greencity/entity/UserAchievement.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package greencity.entity;

import greencity.enums.AchievementStatus;
import lombok.*;

import javax.persistence.*;

@Entity
@Data
@Table(name = "user_achievements")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode
@ToString(exclude = {"user", "achievement"})
@Table(name = "user_achievements")
public class UserAchievement {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -24,10 +23,6 @@ public class UserAchievement {
@ManyToOne(fetch = FetchType.LAZY)
private Achievement achievement;

@Column(nullable = false)
@Enumerated(value = EnumType.STRING)
private AchievementStatus achievementStatus = AchievementStatus.INACTIVE;

@Column
private boolean notified;
}

This file was deleted.

6 changes: 0 additions & 6 deletions dao/src/main/java/greencity/enums/AchievementStatus.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AchievementVO {
private Long id;

private List<AchievementTranslationVO> translations;

private List<UserAchievementVO> userAchievements;

private String title;
private String name;
private String nameEng;
@NotEmpty
private AchievementCategoryVO achievementCategory;

@NotEmpty
private Integer condition;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package greencity.dto.achievement;

import greencity.dto.user.UserVO;
import greencity.enums.AchievementStatus;
import lombok.*;

import javax.validation.constraints.NotEmpty;

@Data
Expand All @@ -21,9 +19,6 @@ public class UserAchievementVO {
@NotEmpty
private AchievementVO achievement;

@NotEmpty
private AchievementStatus achievementStatus;

@NotEmpty
private boolean notified;
}

This file was deleted.

23 changes: 5 additions & 18 deletions service/src/main/java/greencity/mapping/AchievementVOMapper.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
package greencity.mapping;

import greencity.dto.achievement.AchievementTranslationVO;
import greencity.dto.achievement.AchievementVO;
import greencity.dto.achievementcategory.AchievementCategoryVO;
import greencity.dto.language.LanguageVO;
import greencity.entity.Achievement;
import org.modelmapper.AbstractConverter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AchievementVOMapper extends AbstractConverter<Achievement, AchievementVO> {
@Override
protected AchievementVO convert(Achievement achievement) {
List<AchievementTranslationVO> list = new ArrayList<>();
achievement.getTranslations().forEach(achievementTranslation -> list.add(AchievementTranslationVO.builder()
.id(achievementTranslation.getId())
.title(achievementTranslation.getTitle())
.description(achievementTranslation.getDescription())
.message(achievementTranslation.getMessage())
.language(LanguageVO.builder()
.id(achievementTranslation.getLanguage().getId())
.code(achievementTranslation.getLanguage().getCode())
.build())
.build()));
return AchievementVO.builder()
.id(achievement.getId())
.translations(list)
.condition(achievement.getCondition())
.title(achievement.getTitle())
.name(achievement.getName())
.nameEng(achievement.getNameEng())
.condition(achievement.getCondition())
.achievementCategory(AchievementCategoryVO.builder()
.id(achievement.getAchievementCategory().getId())
.name(achievement.getAchievementCategory().getName())
.build())
.condition(achievement.getCondition())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ protected UserVOAchievement convert(User user) {
.userAchievements(user.getUserAchievements() != null ? user.getUserAchievements()
.stream().map(userAchievement -> UserAchievementVO.builder()
.id(userAchievement.getId())
.achievementStatus(userAchievement.getAchievementStatus())
.user(UserVO.builder()
.id(userAchievement.getUser().getId())
.build())
Expand Down
1 change: 0 additions & 1 deletion service/src/main/java/greencity/mapping/UserVOMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ protected UserVO convert(User user) {
.userAchievements(user.getUserAchievements() != null ? user.getUserAchievements()
.stream().map(userAchievement -> UserAchievementVO.builder()
.id(userAchievement.getId())
.achievementStatus(userAchievement.getAchievementStatus())
.user(UserVO.builder()
.id(userAchievement.getUser().getId())
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import greencity.constant.CacheConstants;
import greencity.dto.achievement.AchievementVO;
import greencity.entity.UserAchievement;
import greencity.enums.AchievementStatus;
import greencity.repository.AchievementRepo;
import greencity.repository.UserAchievementRepo;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -44,7 +43,6 @@ public List<AchievementVO> findAll() {
public void findUserAchievement(Long userId, Long achievementId) {
UserAchievement userAchievement = userAchievementRepo
.getUserAchievementByIdAndAchievementId(userId, achievementId);
userAchievement.setAchievementStatus(AchievementStatus.ACTIVE);
userAchievementRepo.save(userAchievement);
}
}
17 changes: 5 additions & 12 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@
import greencity.entity.User;
import greencity.entity.UserAchievement;
import greencity.entity.VerifyEmail;
import greencity.entity.localization.AchievementTranslation;
import greencity.enums.AchievementStatus;
import greencity.enums.EmailNotification;
import greencity.enums.Role;
import greencity.enums.UserStatus;
import greencity.security.dto.ownsecurity.EmployeeSignUpDto;
import greencity.security.dto.ownsecurity.OwnRestoreDto;
import greencity.security.dto.ownsecurity.OwnSignUpDto;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -310,7 +307,6 @@ public static UserVO getUserVOWithData() {
.userAchievements(List.of(
UserAchievementVO.builder()
.id(47L)
.achievementStatus(AchievementStatus.ACTIVE)
.user(UserVO.builder()
.id(13L)
.build())
Expand All @@ -320,7 +316,6 @@ public static UserVO getUserVOWithData() {
.build(),
UserAchievementVO.builder()
.id(39L)
.achievementStatus(AchievementStatus.INACTIVE)
.user(UserVO.builder()
.id(13L)
.build())
Expand Down Expand Up @@ -371,20 +366,18 @@ public static UserProfilePictureDto getUserProfilePictureDto() {
}

public static Achievement getAchievement() {
return new Achievement(1L, Collections.singletonList(getAchievementTranslation()), Collections.emptyList(),
return new Achievement(1L, "ACQUIRED_HABIT_14_DAYS", "Набуття звички протягом 14 днів",
"Acquired habit 14 days", Collections.emptyList(),
new AchievementCategory(), 1);
}

public static AchievementVO getAchievementVO() {
return new AchievementVO(1L, Collections.emptyList(), Collections.emptyList(), new AchievementCategoryVO(), 1);
}

public static AchievementTranslation getAchievementTranslation() {
return new AchievementTranslation(1L, getLanguage(), "Title", "Description", "Message", null);
return new AchievementVO(1L, "ACQUIRED_HABIT_14_DAYS", "Набуття звички протягом 14 днів",
"Acquired habit 14 days", new AchievementCategoryVO(), 1);
}

public static UserAchievement getUserAchievement() {
return new UserAchievement(1L, getUser(), getAchievement(), AchievementStatus.ACTIVE, false);
return new UserAchievement(1L, getUser(), getAchievement(), false);
}

public static EcoNewsAuthorDto getEcoNewsAuthorDto() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,36 @@
package greencity.mapping;

import greencity.ModelUtils;
import greencity.dto.achievement.AchievementTranslationVO;
import greencity.dto.achievement.AchievementVO;
import greencity.dto.achievementcategory.AchievementCategoryVO;
import greencity.dto.language.LanguageVO;
import greencity.entity.Achievement;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
class AchievementVOMapperTest {

@InjectMocks
AchievementVOMapper mapper;
private AchievementVOMapper achievementVOMapper;

@Test
void convert() {
List<AchievementTranslationVO> list = new ArrayList<>();
Achievement achievement = ModelUtils.getAchievement();

achievement.getTranslations().forEach(achievementTranslation -> list.add(AchievementTranslationVO.builder()
.id(achievementTranslation.getId())
.title(achievementTranslation.getTitle())
.description(achievementTranslation.getDescription())
.message(achievementTranslation.getMessage())
.language(LanguageVO.builder()
.id(achievementTranslation.getLanguage().getId())
.code(achievementTranslation.getLanguage().getCode())
.build())
.build()));

AchievementVO expected = AchievementVO.builder()
.id(achievement.getId())
.translations(list)
.title(achievement.getTitle())
.name(achievement.getName())
.nameEng(achievement.getNameEng())
.achievementCategory(AchievementCategoryVO.builder()
.id(achievement.getAchievementCategory().getId())
.name(achievement.getAchievementCategory().getName())
.build())
.condition(achievement.getCondition())
.build();

assertEquals(expected, mapper.convert(achievement));
assertEquals(expected, achievementVOMapper.convert(achievement));
}
}
Loading

0 comments on commit 62683a0

Please sign in to comment.