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

feat(#413): Add more granular options for substance consumption #414

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 25 additions & 45 deletions src/main/java/com/nonononoki/alovoa/config/EventListenerConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nonononoki.alovoa.config;

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

Expand Down Expand Up @@ -83,51 +84,30 @@ public void handleContextRefresh(ApplicationStartedEvent event) {
}

private void setDefaultUserMiscInfo() {
if(userMiscInfoRepo.count() == 0) {
UserMiscInfo drugsTobaccoInfo = new UserMiscInfo();
drugsTobaccoInfo.setValue(UserMiscInfo.DRUGS_TOBACCO);

UserMiscInfo drugsAlcoholInfo = new UserMiscInfo();
drugsAlcoholInfo.setValue(UserMiscInfo.DRUGS_ALCOHOL);

UserMiscInfo drugsCannabisInfo = new UserMiscInfo();
drugsCannabisInfo.setValue(UserMiscInfo.DRUGS_CANNABIS);

UserMiscInfo drugsOtherInfo = new UserMiscInfo();
drugsOtherInfo.setValue(UserMiscInfo.DRUGS_OTHER);


UserMiscInfo relationshipSingleInfo = new UserMiscInfo();
relationshipSingleInfo.setValue(UserMiscInfo.RELATIONSHIP_SINGLE);

UserMiscInfo relationshipTakenInfo = new UserMiscInfo();
relationshipTakenInfo.setValue(UserMiscInfo.RELATIONSHIP_TAKEN);

UserMiscInfo relationshipOpenInfo = new UserMiscInfo();
relationshipOpenInfo.setValue(UserMiscInfo.RELATIONSHIP_OPEN);

UserMiscInfo relationshipOtherInfo = new UserMiscInfo();
relationshipOtherInfo.setValue(UserMiscInfo.RELATIONSHIP_OTHER);


UserMiscInfo kidsNoInfo = new UserMiscInfo();
kidsNoInfo.setValue(UserMiscInfo.KIDS_NO);

UserMiscInfo kidsYesInfo = new UserMiscInfo();
kidsYesInfo.setValue(UserMiscInfo.KIDS_YES);

userMiscInfoRepo.saveAllAndFlush(Arrays.asList(
drugsTobaccoInfo,
drugsAlcoholInfo,
drugsCannabisInfo,
drugsOtherInfo,
relationshipSingleInfo,
relationshipTakenInfo,
relationshipOpenInfo,
relationshipOtherInfo,
kidsNoInfo,
kidsYesInfo));
}
List<UserMiscInfo> userMiscInfoList = new ArrayList<>(Arrays.asList(
new UserMiscInfo(UserMiscInfo.DRUGS_TOBACCO_YES),
new UserMiscInfo(UserMiscInfo.DRUGS_TOBACCO_NO),
new UserMiscInfo(UserMiscInfo.DRUGS_TOBACCO_SOMETIMES),
new UserMiscInfo(UserMiscInfo.DRUGS_ALCOHOL_YES),
new UserMiscInfo(UserMiscInfo.DRUGS_ALCOHOL_NO),
new UserMiscInfo(UserMiscInfo.DRUGS_ALCOHOL_SOMETIMES),
new UserMiscInfo(UserMiscInfo.DRUGS_CANNABIS_YES),
new UserMiscInfo(UserMiscInfo.DRUGS_CANNABIS_NO),
new UserMiscInfo(UserMiscInfo.DRUGS_CANNABIS_SOMETIMES),
new UserMiscInfo(UserMiscInfo.DRUGS_OTHER_YES),
new UserMiscInfo(UserMiscInfo.DRUGS_OTHER_NO),
new UserMiscInfo(UserMiscInfo.DRUGS_OTHER_SOMETIMES),
new UserMiscInfo(UserMiscInfo.RELATIONSHIP_SINGLE),
new UserMiscInfo(UserMiscInfo.RELATIONSHIP_TAKEN),
new UserMiscInfo(UserMiscInfo.RELATIONSHIP_OPEN),
new UserMiscInfo(UserMiscInfo.RELATIONSHIP_OTHER),
new UserMiscInfo(UserMiscInfo.KIDS_NO),
new UserMiscInfo(UserMiscInfo.KIDS_YES)
));

userMiscInfoList.removeIf(u -> userMiscInfoRepo.existsByValue(u.getValue()));

userMiscInfoRepo.saveAllAndFlush(userMiscInfoList);
}

public void setDefaultAdmin() {
Expand Down
33 changes: 28 additions & 5 deletions src/main/java/com/nonononoki/alovoa/entity/user/UserMiscInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@
import com.nonononoki.alovoa.entity.User;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Entity
@NoArgsConstructor
public class UserMiscInfo {

@Transient
public static final long DRUGS_TOBACCO = 1;
public static final long DRUGS_TOBACCO_YES = 1;
@Transient
public static final long DRUGS_ALCOHOL = 2;
public static final long DRUGS_ALCOHOL_YES = 2;
@Transient
public static final long DRUGS_CANNABIS = 3;
public static final long DRUGS_CANNABIS_YES = 3;
@Transient
public static final long DRUGS_OTHER = 4;
public static final long DRUGS_OTHER_YES = 4;

@Transient
public static final long RELATIONSHIP_SINGLE = 11;
@Transient
Expand All @@ -44,6 +46,24 @@ public class UserMiscInfo {
@Transient
public static final long KIDS_YES = 22;

@Transient
public static final long DRUGS_TOBACCO_NO = 31;
@Transient
public static final long DRUGS_ALCOHOL_NO = 32;
@Transient
public static final long DRUGS_CANNABIS_NO = 33;
@Transient
public static final long DRUGS_OTHER_NO = 34;

@Transient
public static final long DRUGS_TOBACCO_SOMETIMES = 41;
@Transient
public static final long DRUGS_ALCOHOL_SOMETIMES = 42;
@Transient
public static final long DRUGS_CANNABIS_SOMETIMES = 43;
@Transient
public static final long DRUGS_OTHER_SOMETIMES = 44;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -55,4 +75,7 @@ public class UserMiscInfo {
@Column(unique=true)
private long value;

public UserMiscInfo(long value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

public interface UserMiscInfoRepository extends JpaRepository<UserMiscInfo, Long> {
UserMiscInfo findByValue(long value);

boolean existsByValue(long value);
}

19 changes: 19 additions & 0 deletions src/main/java/com/nonononoki/alovoa/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.security.*;
import java.util.List;
import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -55,6 +56,12 @@ public class UserService {
private static final String MIME_WAV = "wav";
private static final String MIME_MPEG = "mpeg";
private static final String MIME_MP3 = "mp3";

private final Set<Long> drugsAlcohol = new HashSet<>(Set.of(UserMiscInfo.DRUGS_ALCOHOL_YES, UserMiscInfo.DRUGS_ALCOHOL_SOMETIMES, UserMiscInfo.DRUGS_ALCOHOL_NO));
private final Set<Long> drugsTobacco = new HashSet<>(Set.of(UserMiscInfo.DRUGS_TOBACCO_YES, UserMiscInfo.DRUGS_TOBACCO_SOMETIMES, UserMiscInfo.DRUGS_TOBACCO_NO));
private final Set<Long> drugsCannabis = new HashSet<>(Set.of(UserMiscInfo.DRUGS_CANNABIS_YES, UserMiscInfo.DRUGS_CANNABIS_SOMETIMES, UserMiscInfo.DRUGS_CANNABIS_NO));
private final Set<Long> drugsOther = new HashSet<>(Set.of(UserMiscInfo.DRUGS_OTHER_YES, UserMiscInfo.DRUGS_OTHER_SOMETIMES, UserMiscInfo.DRUGS_OTHER_NO));

@Autowired
private AuthService authService;
@Autowired
Expand Down Expand Up @@ -513,6 +520,14 @@ public Set<UserMiscInfo> updateUserMiscInfo(long infoValue, boolean activated) t
} else if (infoValue >= UserMiscInfo.RELATIONSHIP_SINGLE && infoValue <= UserMiscInfo.RELATIONSHIP_OTHER) {
list.removeIf(o -> o.getValue() != infoValue && o.getValue() >= UserMiscInfo.RELATIONSHIP_SINGLE
&& o.getValue() <= UserMiscInfo.RELATIONSHIP_OTHER);
} else if (drugsAlcohol.contains(infoValue)) {
list.removeIf(conditionToRemoveIfExistent(drugsAlcohol, infoValue));
} else if (drugsTobacco.contains(infoValue)) {
list.removeIf(conditionToRemoveIfExistent(drugsAlcohol, infoValue));
} else if (drugsCannabis.contains(infoValue)) {
list.removeIf(conditionToRemoveIfExistent(drugsCannabis, infoValue));
} else if (drugsOther.contains(infoValue)) {
list.removeIf(conditionToRemoveIfExistent(drugsOther, infoValue));
}

} else {
Expand All @@ -523,6 +538,10 @@ public Set<UserMiscInfo> updateUserMiscInfo(long infoValue, boolean activated) t
return list;
}

private Predicate<UserMiscInfo> conditionToRemoveIfExistent(final Set<Long> options, final long infoValue) {
return c -> options.contains(c.getValue()) && c.getValue() != infoValue;
}

public void addInterest(String value) throws AlovoaException {
User user = authService.getCurrentUser(true);

Expand Down
Loading