Skip to content

Commit

Permalink
Merge pull request #6419 from ita-social-projects/change-structure-ac…
Browse files Browse the repository at this point in the history
…heievements

Change structure achievements
  • Loading branch information
ospodaryk authored Oct 4, 2023
2 parents 8fa7adb + 78a9f09 commit 5146cec
Show file tree
Hide file tree
Showing 43 changed files with 186 additions and 432 deletions.
39 changes: 0 additions & 39 deletions core/src/main/java/greencity/controller/AchievementController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import greencity.constant.HttpStatuses;
import greencity.dto.achievement.AchievementDTO;
import greencity.dto.achievement.AchievementNotification;
import greencity.dto.achievement.AchievementVO;
import greencity.dto.user.UserVO;
import greencity.enums.AchievementCategoryType;
import greencity.enums.AchievementType;
import greencity.service.AchievementService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand Down Expand Up @@ -47,39 +43,4 @@ public AchievementController(AchievementService achievementService) {
public ResponseEntity<List<AchievementVO>> getAll() {
return ResponseEntity.status(HttpStatus.OK).body(achievementService.findAll());
}

/**
* Method notifies of the achievement.
*
* @param userId of {@link UserVO}
* @return list {@link AchievementNotification}
*/
@ApiOperation(value = "Get all the achievements that need to notify.")
@ApiResponses(value = {
@ApiResponse(code = 200, message = HttpStatuses.OK),
@ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST),
@ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED),
})
@GetMapping("/notification/{userId}")
public ResponseEntity<List<AchievementNotification>> getNotification(@PathVariable Long userId) {
return ResponseEntity.status(HttpStatus.OK).body(achievementService.findAchievementsWithStatusActive(userId));
}

/**
* Method for achievement calculation.
*/
@ApiOperation(value = "Calculate achievements.")
@ApiResponses(value = {
@ApiResponse(code = 200, message = HttpStatuses.OK),
@ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST),
@ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED),
})
@PostMapping("/calculate-achievement")
public ResponseEntity<HttpStatus> calculateAchievements(@RequestParam Long id,
@RequestParam AchievementType setter,
@RequestParam AchievementCategoryType socialNetwork,
@RequestParam int size) {
achievementService.calculateAchievements(id, setter, socialNetwork, size);
return ResponseEntity.ok().build();
}
}
19 changes: 6 additions & 13 deletions core/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import greencity.constant.AppConstant;
import greencity.dto.PageableAdvancedDto;
import greencity.dto.achievement.AchievementPostDto;
import greencity.dto.achievement.AchievementTranslationVO;

import greencity.dto.achievement.AchievementVO;
import greencity.dto.achievement.UserAchievementVO;
import greencity.dto.achievementcategory.AchievementCategoryDto;
import greencity.dto.achievementcategory.AchievementCategoryVO;
import greencity.dto.advice.AdvicePostDto;
Expand Down Expand Up @@ -582,25 +581,19 @@ public static ShoppingListItemPostDto getShoppingListItemPostDto() {
return new ShoppingListItemPostDto(getLanguageTranslationsDTOs(), new ShoppingListItemRequestDto(1L));
}

public static List<AchievementTranslationVO> getAchievementTranslationVOS() {
return Arrays.asList(
new AchievementTranslationVO(1L, getLanguageVO(), "title", "description", "message"),
new AchievementTranslationVO(2L, getLanguageVO(), "title", "description", "message"),
new AchievementTranslationVO(3L, getLanguageVO(), "title", "description", "message"));
}

public static AchievementCategoryDto getAchievementCategoryDto() {
return new AchievementCategoryDto("name");
}

public static AchievementPostDto getAchievementPostDto() {
return new AchievementPostDto(getAchievementTranslationVOS(), getAchievementCategoryDto(), 1);
return new AchievementPostDto("ACQUIRED_HABIT_14_DAYS", "Набуття звички протягом 14 днів",
"Acquired habit 14 days", getAchievementCategoryDto(), 1);
}

public static AchievementVO getAchievementVO() {
return new AchievementVO(1L, getAchievementTranslationVOS(),
Collections.singletonList(new UserAchievementVO()),
new AchievementCategoryVO(1L, "name", null, null), 1);
return new AchievementVO(1L, "ACQUIRED_HABIT_14_DAYS", "Набуття звички протягом 14 днів",
"Acquired habit 14 days",
new AchievementCategoryVO(1L, "name"), 1);
}

public static UserShoppingListItem getUserShoppingListItem() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package greencity.controller;

import greencity.enums.AchievementCategoryType;
import greencity.enums.AchievementType;
import greencity.service.AchievementService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -45,22 +44,4 @@ void findAllTest() throws Exception {
verify(achievementService).findAll();
}

@Test
void calculateAchievements() throws Exception {
mockMvc.perform(post(achievementLink + "/calculate-achievement"
+ "?id=" + 1L
+ "&setter=" + AchievementType.INCREMENT
+ "&socialNetwork=" + AchievementCategoryType.ECO_NEWS
+ "&size=" + 1)).andExpect(status().isOk());
verify(achievementService).calculateAchievements(1L, AchievementType.INCREMENT,
AchievementCategoryType.ECO_NEWS, 1);
}

@Test
void getNotificationTest() throws Exception {
mockMvc.perform(get(achievementLink
+ "/notification/{userId}", 1)).andExpect(status().isOk());
verify(achievementService).findAchievementsWithStatusActive(1L);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ void getAllAchievementSearchByQueryTest() throws Exception {

@Test
void saveAchievementTest() throws Exception {
// String accessToken = "accessToken";
// HttpHeaders headers = new HttpHeaders();
// headers.set(AUTHORIZATION, accessToken);
AchievementPostDto achievementPostDto = ModelUtils.getAchievementPostDto();
String content = objectMapper.writeValueAsString(achievementPostDto);
this.mockMvc.perform(post(link)
Expand Down
11 changes: 7 additions & 4 deletions dao/src/main/java/greencity/entity/Achievement.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package greencity.entity;

import greencity.entity.localization.AchievementTranslation;

import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
Expand Down Expand Up @@ -32,8 +30,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 Down
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 @@ -25,10 +25,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;
}
5 changes: 0 additions & 5 deletions dao/src/main/java/greencity/entity/UserAchievement.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package greencity.entity;

import greencity.enums.AchievementStatus;
import javax.persistence.*;

import lombok.*;
Expand All @@ -25,10 +24,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.

15 changes: 6 additions & 9 deletions dao/src/main/java/greencity/repository/AchievementRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@
@Repository
public interface AchievementRepo extends JpaRepository<Achievement, Long> {
/**
* Method returns {@link Achievement} by search query and page.
* Searches for achievements based on a query string and returns a paginated
* result.
*
* @param paging {@link Pageable}.
* @param query query to search.
* @return list of {@link Achievement}.
* @author Orest Mamchuk
* @param paging A Pageable object containing the pagination information (e.g.,
* page number, size, sort order).
* @param query The search query string to filter achievements.
* @return A Page of Achievement objects that match the search query.
*/
@Query("SELECT DISTINCT a FROM Achievement a "
+ "JOIN AchievementTranslation at on at.achievement.id = a.id "
+ "WHERE CONCAT(a.id,'') LIKE LOWER(CONCAT('%', :query, '%')) "
+ "OR LOWER(at.title) LIKE LOWER(CONCAT('%', :query, '%'))"
+ "OR LOWER(at.description) LIKE LOWER(CONCAT('%', :query, '%'))"
+ "OR LOWER(at.message) LIKE LOWER(CONCAT('%', :query, '%'))"
+ "OR LOWER(a.achievementCategory.name) LIKE LOWER(CONCAT('%', :query, '%'))"
+ "OR CONCAT(a.condition, ' ') LIKE LOWER(CONCAT('%', :query, '%'))")
Page<Achievement> searchAchievementsBy(Pageable paging, String query);
Expand Down

This file was deleted.

10 changes: 10 additions & 0 deletions dao/src/main/java/greencity/repository/UserAchievementRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface UserAchievementRepo extends JpaRepository<UserAchievement, Long> {
Expand All @@ -16,4 +17,13 @@ public interface UserAchievementRepo extends JpaRepository<UserAchievement, Long
*/
@Query(value = "FROM UserAchievement u WHERE u.user.id =:userId AND u.achievement.id =:achievementId")
UserAchievement getUserAchievementByIdAndAchievementId(Long userId, Long achievementId);

/**
* Retrieves a list of UserAchievement objects associated with a given user ID.
*
* @param userId The unique identifier of the user for whom to fetch the
* achievements.
* @return A list of UserAchievement objects related to the specified user ID.
*/
List<UserAchievement> getUserAchievementByUserId(Long userId);
}
3 changes: 3 additions & 0 deletions dao/src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@
<include file="db/changelog/logs/ch-delete-from-fact_of_the_day_translations-Spodaryk.xml"/>
<include file="db/changelog/logs/ch-delete-from-tag_translations-Spodaryk.xml"/>
<include file="db/changelog/logs/ch-delete-from-languages-Spodaryk.xml"/>
<include file="db/changelog/logs/ch-add-column-achievements-Spodaryk.xml"/>
<include file="db/changelog/logs/ch-drop-column-user_achievements-Spodaryk.xml"/>
<include file="db/changelog/logs/ch-delete-table-achievement_translations-Spodaryk.xml"/>
<include file="db/changelog/logs/ch-delete-values-employee_authorities-mapping-Spodaryk.xml"/>
<include file="db/changelog/logs/ch-delete-values-positions_authorities-mapping-Spodaryk.xml"/>
<include file="db/changelog/logs/ch-add-values-_authorities-mapping-Spodaryk.xml"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<changeSet id="Spodaryk-22" author="Oksana Spodaryk">
<preConditions onFail="MARK_RAN">
<and>
<tableExists tableName="achievements"/>
<not>
<columnExists tableName="achievements" columnName="title"/>
</not>
<not>
<columnExists tableName="achievements" columnName="name"/>
</not>
<not>
<columnExists tableName="achievements" columnName="name_eng"/>
</not>
<not>
<columnExists tableName="achievements" columnName="score"/>
</not>
</and>
</preConditions>

<addColumn tableName="achievements">
<column name="title" type="VARCHAR(255)">
<constraints nullable="true"/>
</column>
</addColumn>
<addColumn tableName="achievements">
<column name="name" type="VARCHAR(255)">
<constraints nullable="true"/>
</column>
</addColumn>
<addColumn tableName="achievements">
<column name="name_eng" type="VARCHAR(255)">
<constraints nullable="true"/>
</column>
</addColumn>
<addColumn tableName="achievements">
<column name="score" type="INT">
<constraints nullable="true"/>
</column>
</addColumn>
</changeSet>


</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<changeSet id="Spodaryk-25" author="Oksana Spodaryk">
<preConditions onFail="MARK_RAN">
<tableExists tableName="achievement_translations"/>
</preConditions>
<dropTable tableName="achievement_translations"/>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<changeSet id="Spodaryk-24" author="Oksana Spodaryk">
<preConditions onFail="MARK_RAN">
<columnExists tableName="user_achievements" columnName="achievement_status"/>
</preConditions>
<dropColumn tableName="user_achievements" columnName="achievement_status"/>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public final class ErrorMessage {
"Count of tags should be at least one but not more three";
public static final String TOKEN_FOR_RESTORE_IS_INVALID = "Token is null or it doesn't exist.";
public static final String ACHIEVEMENT_NOT_DELETED = "Achievement not deleted ";
public static final String ACHIEVEMENT_NOT_FOUND_BY_ID = "The name does not exist by this id: ";
public static final String ACHIEVEMENT_NOT_FOUND_BY_ID = "The achievement does not exist by this id: ";
public static final String PAGE_INDEX_IS_MORE_THAN_TOTAL_PAGES = "Page index is more than total pages: ";
public static final String MULTIPART_FILE_BAD_REQUEST =
"Can`t convert To Multipart Image. Bad inputed image string : ";
Expand Down
Loading

0 comments on commit 5146cec

Please sign in to comment.