Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #763 from SE-TINF22B6/homepage
Browse files Browse the repository at this point in the history
Backend Changes
  • Loading branch information
maxschwinghammer authored May 29, 2024
2 parents 92142a1 + 58c107c commit 309abec
Show file tree
Hide file tree
Showing 16 changed files with 19 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ResponseEntity<?> signup(@Valid @RequestBody SignupRequest signupRequest)
signupRequest.getEmail(),
passwordEncoder.encode(signupRequest.getPassword()), null,true);

User newUser = new User(null, "new User", null, newAccount);
User newUser = new User("new User", null, newAccount);

accountRepository.save(newAccount);

Expand Down Expand Up @@ -121,5 +121,4 @@ public ResponseEntity<?> tokenValidation (@Valid @RequestBody TokenValidationReq
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ public Course update(@PathVariable Long id, @RequestBody CourseProposal proposal
public void delete(@PathVariable Long id) {
service.delete(id);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ public EventThreadViewProposal create(@RequestBody CreateEventPostProposal propo
return service.create(proposal);
}

@PutMapping("/update-event/{id}")
public EventThreadViewProposal update(@PathVariable Long id, @RequestBody UpdateEventPostProposal proposal) {
return service.update(id, proposal);
}

@PostMapping("/create-comment")
public EventCommentThreadViewProposal create(@RequestBody CreateEventCommentProposal proposal) {
return service.create(proposal);
Expand Down
19 changes: 5 additions & 14 deletions src/main/java/de/tinf22b6/dhbwhub/mapper/EventMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,29 @@
import de.tinf22b6.dhbwhub.proposal.simplified_models.*;

public class EventMapper {
public static EventPost mapToModel(CreateEventPostProposal proposal, User user, Picture picture){
public static EventPost mapToModel(CreateEventPostProposal proposal){
return new EventPost(
proposal.getTitle(),
proposal.getDescription(),
proposal.getLocation().getLocation(),
proposal.getLocation().getLatitude(),
proposal.getLocation().getLongitude(),
proposal.getTimestamp(),
proposal.getStartdate(),
proposal.getEnddate(),
0,
picture,
user
0
);
}

public static EventPost mapToModel(UpdateEventPostProposal proposal, EventPost post, Picture picture){
public static EventPost mapToModel(UpdateEventPostProposal proposal, EventPost post){
return new EventPost(
proposal.getTitle(),
proposal.getDescription(),
proposal.getLocation().getLocation(),
proposal.getLocation().getLatitude(),
proposal.getLocation().getLongitude(),
post.getTimestamp(),
proposal.getStartdate(),
proposal.getEnddate(),
post.getLikes(),
picture,
post.getUser()
post.getLikes()
);
}

Expand All @@ -43,12 +37,9 @@ public static EventPost mapToModel(EventPost post, int likes) {
post.getLocation(),
post.getLatitude(),
post.getLongitude(),
post.getTimestamp(),
post.getStartdate(),
post.getEnddate(),
likes,
post.getPicture(),
post.getUser()
likes
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public static Friendship mapToFriendship(User requester, User receiver){
requester,
receiver);
}
}
}
11 changes: 1 addition & 10 deletions src/main/java/de/tinf22b6/dhbwhub/mapper/NotificationMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,4 @@ public static CommentLikeNotification mapToCommentLikeNotification(Comment comme
false
);
}

public static EventCommentLikeNotification mapToEventCommentLikeNotification(EventComment eventComment, User user) {
return new EventCommentLikeNotification(
eventComment.getEventPost().getUser(),
eventComment.getEventPost(),
user,
false
);
}
}
}
1 change: 0 additions & 1 deletion src/main/java/de/tinf22b6/dhbwhub/mapper/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
public class UserMapper {
public static User mapToModel(UserProposal proposal) {
return new User(
proposal.getAge(),
proposal.getDescription(),
proposal.getCourse() != null ? CourseMapper.mapToModel(proposal.getCourse()) : null,
proposal.getAccount() != null ? AccountMapper.mapToModel(proposal.getAccount()) : null
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/de/tinf22b6/dhbwhub/model/EventPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,10 @@ public class EventPost {

private final Double longitude;

private final Date timestamp;

private final Date startdate;

private final Date enddate;

private final int likes;

@JoinColumn(name = "picture_id")
@ManyToOne(cascade = CascadeType.PERSIST)
private final Picture picture;

@JoinColumn(name = "user_id")
@ManyToOne(cascade = CascadeType.PERSIST)
private final User user;

}
2 changes: 0 additions & 2 deletions src/main/java/de/tinf22b6/dhbwhub/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class User {
@Column(name = "user_id")
private Long id;

private final Integer age;

private final String description;

@JoinColumn(name = "course_id")
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/de/tinf22b6/dhbwhub/proposal/UserProposal.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
@AllArgsConstructor
@NoArgsConstructor
public class UserProposal {
private Integer age;

private String description;

private CourseProposal course;
Expand Down
73 changes: 2 additions & 71 deletions src/main/java/de/tinf22b6/dhbwhub/service/EventServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,31 @@

import de.tinf22b6.dhbwhub.exception.NoSuchEntryException;
import de.tinf22b6.dhbwhub.mapper.EventMapper;
import de.tinf22b6.dhbwhub.mapper.EventTagMapper;
import de.tinf22b6.dhbwhub.mapper.NotificationMapper;
import de.tinf22b6.dhbwhub.mapper.PictureMapper;
import de.tinf22b6.dhbwhub.model.*;
import de.tinf22b6.dhbwhub.model.log_tables.LikeLogtableEventComment;
import de.tinf22b6.dhbwhub.model.log_tables.LikeLogtableEventPost;
import de.tinf22b6.dhbwhub.model.notification_tables.EventCommentLikeNotification;
import de.tinf22b6.dhbwhub.proposal.simplified_models.*;
import de.tinf22b6.dhbwhub.repository.*;
import de.tinf22b6.dhbwhub.service.interfaces.EventService;
import jakarta.persistence.EntityExistsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class EventServiceImpl implements EventService {
private final EventRepository repository;
private final UserRepository userRepository;
private final PictureRepository pictureRepository;
private final LogtableRepository logtableRepository;
private final NotificationRepository notificationRepository;

public EventServiceImpl(@Autowired EventRepository repository,
@Autowired UserRepository userRepository,
@Autowired PictureRepository pictureRepository,
@Autowired LogtableRepository logtableRepository,
@Autowired NotificationRepository notificationRepository) {
@Autowired LogtableRepository logtableRepository) {
this.repository = repository;
this.userRepository = userRepository;
this.pictureRepository = pictureRepository;
this.logtableRepository = logtableRepository;
this.notificationRepository = notificationRepository;
}

@Override
Expand Down Expand Up @@ -77,12 +65,7 @@ public List<HomepageEventPreviewProposal> getHomepageEvents() {

@Override
public EventThreadViewProposal create(CreateEventPostProposal proposal) {
// Creating the Post itself
User user = userRepository.findByAccountId(proposal.getAccountId());
Picture picture = proposal.getPostImage().length != 0 ?
pictureRepository.save(PictureMapper.mapToModelPost(proposal.getPostImage())): null;

EventPost post = repository.save(EventMapper.mapToModel(proposal,user,picture));
EventPost post = repository.save(EventMapper.mapToModel(proposal));

// Creating Tags after the Post is created
Arrays.stream(proposal.getTags()).forEach(t -> {
Expand All @@ -109,52 +92,6 @@ public int getAmountOfComments(Long id) {
return repository.getAmountOfComments(id);
}

@Override
public EventThreadViewProposal update(Long id, UpdateEventPostProposal proposal) {
EventPost initialPost = getEventPost(id);
Picture picture;

byte[] proposalImageData = proposal.getPostImage() != null? proposal.getPostImage() : new byte[0];
byte[] initialImageData = initialPost.getPicture() != null? initialPost.getPicture().getImageData() : new byte[0];
// Check if Picture has changed
if (proposalImageData.length == 0 && initialImageData.length != 0) {
pictureRepository.delete(initialPost.getPicture().getId());
picture = null;
}
else if (!Arrays.equals(initialImageData, proposalImageData)) {
pictureRepository.delete(initialPost.getPicture().getId());
picture = pictureRepository.save(PictureMapper.mapToModelPost(proposalImageData));
} else {
picture = initialPost.getPicture();
}

// Update post
EventPost updatedPost = EventMapper.mapToModel(proposal, initialPost, picture);
updatedPost.setId(id);

EventPost post = repository.save(updatedPost);

/* Check if Tags changed
formerTags = Tags in the database
proposedTags = Tags proposed
* */
List<EventTag> formerTags = repository.findTagsByPostId(id);
List<String> proposedTags = new ArrayList<>(Arrays.stream(proposal.getTags()).toList());

for (EventTag eventTag : formerTags) {
if (proposedTags.contains(eventTag.getTag())) {
proposedTags.remove(eventTag.getTag());
} else {
repository.deleteEventTag(eventTag.getId());
}
}
proposedTags.forEach(t -> {
if( t != null) repository.save(EventTagMapper.mapToModel(post, t));
});

return getEventThreadView(updatedPost.getId());
}

@Override
public void deletePost(Long id) {
getEventPost(id);
Expand Down Expand Up @@ -235,12 +172,6 @@ public int adjustCommentLikes(LikeEventCommentProposal likeEventCommentProposal,
throw new EntityExistsException("Entity already exists!");
}
logtableRepository.saveEventComment(likeLogtableEventComment);

if (!Objects.equals(eventComment.getUser().getId(), user.getId())) {
EventCommentLikeNotification notification = NotificationMapper.mapToEventCommentLikeNotification(eventComment, user);
notification.setAccumulatedId(null);
notificationRepository.saveEventCommentLikeNotification(notification);
}
} else {
likes = eventComment.getLikes() == 0 ? 0 : eventComment.getLikes() - 1;
LikeLogtableEventComment likeLogtableEventComment = logtableRepository.findEventComment(likeEventCommentProposal.getEventCommentId(),likeEventCommentProposal.getUserId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public interface EventService {
List<HomepageEventPreviewProposal> getHomepageEvents();
EventThreadViewProposal create(CreateEventPostProposal proposal);
EventThreadViewProposal getEventThreadView(Long id);
EventThreadViewProposal update(Long id, UpdateEventPostProposal proposal);
void deletePost(Long id);
void deleteComment(Long id);
void deleteTag(Long id);
Expand Down
44 changes: 6 additions & 38 deletions src/test/java/de/tinf22b6/dhbwhub/AbstractApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ protected Course createDefaultCourse2() {
}

protected User createDefaultUser() {
return new User(19, "Ich studiere Informatik", createDefaultCourse(), createDefaultAccount());
return new User("Ich studiere Informatik", createDefaultCourse(), createDefaultAccount());
}

protected User createDefaultUser2() {
return new User(21, "Ich studiere Jura", createDefaultCourse2(), createDefaultAccount2());
return new User("Ich studiere Jura", createDefaultCourse2(), createDefaultAccount2());
}

protected Friendship createDefaultFriendship() {
Expand Down Expand Up @@ -132,11 +132,7 @@ protected CourseProposal createDefaultCourseProposal() {
}

protected UserProposal createDefaultUserProposal() {
return new UserProposal(19, "Ich studiere Informatik", createDefaultCourseProposal(), createDefaultAccountProposal());
}

protected FriendshipProposal createDefaultFriendshipProposal() {
return new FriendshipProposal(createDefaultAccountProposal(), createDefaultAccountProposal(), false);
return new UserProposal("Ich studiere Informatik", createDefaultCourseProposal(), createDefaultAccountProposal());
}

protected PostProposal createDefaultPostProposal() {
Expand Down Expand Up @@ -184,15 +180,15 @@ protected UpdatePostProposal createDefaultUpdatePostProposal() {
}

protected EventPost createDefaultEventPost() {
return new EventPost("Titel 1", "Beschreibung 1", "Location 1", 1.0, 2.0, new Date(1478979207L), new Date(1478979208L), new Date(1478979209L), 1, createDefaultPicture(), createDefaultUser());
return new EventPost("Titel 1", "Beschreibung 1", "Location 1", 1.0, 2.0, new Date(1478979208L), new Date(1478979209L), 1);
}

protected EventPost createUpdatedDefaultEventPost() {
return new EventPost("Titel 1", "Beschreibung 1", "Location 1", 1.0, 2.0, new Date(1478979207L), new Date(1478979208L), new Date(1478979209L), 2, createDefaultPicture(), createDefaultUser());
return new EventPost("Titel 1", "Beschreibung 1", "Location 1", 1.0, 2.0, new Date(1478979208L), new Date(1478979209L), 2);
}

protected EventPost createUpdatedDefaultEventPost2() {
return new EventPost("Titel 1", "Beschreibung 1", "Location 1", 1.0, 2.0, new Date(1478979207L), new Date(1478979208L), new Date(1478979209L), 0, createDefaultPicture(), createDefaultUser());
return new EventPost("Titel 1", "Beschreibung 1", "Location 1", 1.0, 2.0, new Date(1478979208L), new Date(1478979209L), 0);
}

protected EventComment createDefaultEventComment() {
Expand All @@ -218,10 +214,6 @@ protected CreateEventPostProposal createDefaultCreateEventPostProposal() {
return new CreateEventPostProposal("Titel 1", "Beschreibung 1", createDefaultLocationProposal(), new Date(1478979207L), new Date(1478979208L), new Date(1478979209L), new String[]{"Tag 1", "Tag 2"}, createDefaultPicture().getImageData(), 1L);
}

protected UpdateEventPostProposal createDefaultUpdateEventPostProposal() {
return new UpdateEventPostProposal("Titel 1", "Beschreibung 1", createDefaultLocationProposal(), new Date(1478979207L), new Date(1478979208L), new String[]{"Tag 1", "Tag 2"}, createDefaultPicture().getImageData());
}

protected EventThreadViewProposal createDefaultEventThreadViewProposal() {
return new EventThreadViewProposal(1L, "Titel 1", "Beschreibung 1", List.of("Tag 1", "Tag 2"), createDefaultLocationProposal(), 0, 0, new Date(1478979208L), new Date(1478979209L), List.of(createDefaultEventCommentThreadViewProposal(),createDefaultEventCommentThreadViewProposal()));
}
Expand All @@ -238,14 +230,6 @@ protected EventCommentThreadViewProposal createDefaultEventCommentThreadViewProp
return new EventCommentThreadViewProposal(1L, 1L, "Beschreibung 1", "Maxim", 1L, createDefaultPicture().getImageData(), new Date(1478979207L), 0);
}

protected CreateEventCommentProposal createDefaultCreateEventCommentProposal() {
return new CreateEventCommentProposal(1L, 1L, "Beschreibung 1", new Date(1478979207L));
}

protected UpdateEventCommentProposal createDefaultUpdateEventCommentProposal() {
return new UpdateEventCommentProposal("Beschreibung 1");
}

protected FollowUserProposal createDefaultFollowUserProposal() {
return new FollowUserProposal(0L, 1L);
}
Expand Down Expand Up @@ -294,22 +278,6 @@ protected DeleteNotificationProposal createDeleteNotificationProposal() {
return new DeleteNotificationProposal(1L, null, "Type-Post-Like");
}

protected DeleteNotificationProposal createDeleteNotificationProposal2() {
return new DeleteNotificationProposal(1L, null, "Type-Post-Comment");
}

protected DeleteNotificationProposal createDeleteNotificationProposal3() {
return new DeleteNotificationProposal(1L, null, "Type-Comment-Like");
}

protected DeleteNotificationProposal createDeleteNotificationProposal4() {
return new DeleteNotificationProposal(1L, null, "Type-Event-Comment-Like");
}

protected DeleteNotificationProposal createDeleteNotificationProposal5() {
return new DeleteNotificationProposal(1L, null, "Type-Follow");
}

protected UserLikes createDefaultUserLikes() {
return new UserLikes(1L, List.of(1L, 2L), List.of(2L, 4L), List.of(1L, 3L), List.of(2L));
}
Expand Down
Loading

0 comments on commit 309abec

Please sign in to comment.