Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@CrossOrigin
@RestController
public class FileAttachmentController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@CrossOrigin
@RestController
public class FileController {

Expand All @@ -40,14 +40,6 @@ public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
file.getContentType(), file.getSize());
}

@PostMapping("/uploadMultipleFiles")
public List<UploadFileResponse> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
return Arrays.asList(files)
.stream()
.map(file -> uploadFile(file))
.collect(Collectors.toList());
}

@GetMapping("/downloadFile/{fileName:.+}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
// Load file as Resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public ResponseEntity<Message> editMessage(@PathVariable Long messageId, @Reques
}
}

@DeleteMapping("messages/{messageId")
@DeleteMapping("/messages/{messageId}")
public ResponseEntity<Boolean> deleteMessage(@PathVariable Long messageId) {
try {
verifyMessage(messageId);
Expand All @@ -88,4 +88,8 @@ private void verifyMessage(Long messageId) {
throw new ResourceNotFoundException("Message " + messageId + " not found.");
}
}
@PatchMapping("/messages/{messageId}")
private ResponseEntity<Message> updateMessageSender(){
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

@RestController
public class PollController {
Expand All @@ -33,36 +34,12 @@ public PollController(PollService pollService, ChatService chatService) {
this.pollService = pollService;
}

// @PutMapping("/poll/{pollId}")
// public ResponseEntity<Poll> getPollByPollId(@PathVariable Long pollId) {
// try {
// verifyPollById(pollId);
// return new ResponseEntity<>(pollService.getPollById(pollId), HttpStatus.OK);
// } catch (ResourceNotFoundException ex) {
// return new ResponseEntity<>(HttpStatus.NOT_FOUND);
// }
// }
//
// @PatchMapping("/poll/{pollId}")
// public ResponseEntity<Poll> addOptionToPoll(@PathVariable Long pollId, Long optionId) {
// try {
// verifyPollById(pollId);
// return new ResponseEntity<>(pollService.addOptionToPoll(pollId, optionId), HttpStatus.OK);
// } catch (ResourceNotFoundException ex) {
// return new ResponseEntity<>(HttpStatus.NOT_FOUND);
// }
// }

@GetMapping(value="/chat/{id}/polls")
public ResponseEntity<Iterable<Poll>> getAllPolls(@PathVariable Long id) {

return new ResponseEntity<>(pollService.getPollByChatId(id), HttpStatus.OK);
}
// @RequestMapping(value="/chat/{id}/polls/{pollId}", method= RequestMethod.GET)
// public ResponseEntity<?> getPoll(@PathVariable Long id, @PathVariable Long pollId) {
//
// return new ResponseEntity<>(pollService.show(id, pollId), HttpStatus.OK);
// }


@RequestMapping(value="/chat/{chatId}/polls", method=RequestMethod.POST)
public ResponseEntity<?> createPoll(@RequestBody Poll poll, @PathVariable Long chatId) {
Expand All @@ -72,13 +49,19 @@ public ResponseEntity<?> createPoll(@RequestBody Poll poll, @PathVariable Long c
return new ResponseEntity<>(pollService.create(poll), HttpStatus.CREATED);
}

@GetMapping("/poll/{pollId}")
public ResponseEntity<HashMap<String, Integer>> countPollVotes(@PathVariable Long pollId) {
return new ResponseEntity<>(pollService.getPollVotes(pollId), HttpStatus.OK);
}

@RequestMapping(value="/chat/{id}/polls", method=RequestMethod.PUT)
public ResponseEntity<?> updatePoll(@RequestBody Poll poll,@PathVariable Long id, @PathVariable Long pollId) {
return new ResponseEntity<>(pollService.update(pollId,poll), HttpStatus.CREATED);
}
@RequestMapping(value="/chat/{id}/polls", method=RequestMethod.DELETE)
public ResponseEntity<?> deletePoll( @PathVariable Long id) {
try {
verifyPollById(id);
pollService.delete(id);
return new ResponseEntity<>(HttpStatus.OK);
}catch(ResourceNotFoundException ex){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public VoteController(VoteService voteService, PollService pollService) {
this.pollService = pollService;
}

@RequestMapping(value = "/chats/{chatId}/polls/{pollId}/votes", method = RequestMethod.POST)
@RequestMapping(value = "vote/poll/{pollId}/chat/{chatId}", method = RequestMethod.POST)
public ResponseEntity<Vote> createVote(
@PathVariable Long chatId,
@PathVariable Long pollId,
@RequestBody Vote vote) {
vote = voteService.create(chatId, pollId, vote);
@RequestParam Long userId,
@RequestParam Long optionId) {
Vote vote = voteService.create(chatId, pollId, userId, optionId);
HttpHeaders responseHeaders = new HttpHeaders(); // Set the headers for the newly created resource
responseHeaders.setLocation(ServletUriComponentsBuilder
.fromCurrentRequest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface MessageDTO {
String getMessageBody();
UserDTO getSender();
ChatDTO getDestinationChat();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ public class Message {
private Date timeStamp = new Date();
private String messageBody;


@ManyToOne
private User sender;

@ManyToOne
private Chat destinationChat;



public Message() {
}

Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/groupfour/chatapp/chatapp/models/Poll.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,19 @@
public class Poll {
@Id
@GeneratedValue
@Column(name = "POLL_ID")
private Long pollId;


@Column(name = "QUESTION")
private String pollQuestion;

private Date timeStamp = new Date();
@JoinColumn(name = "POLL_ID")

@Size(max = 3)
@OrderBy
@OneToMany(cascade = CascadeType.ALL)
private Set<Option> options;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "POLL_ID")
@OrderBy
private Set<Vote> votes;

@ManyToOne
private Chat chat;

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/groupfour/chatapp/chatapp/models/Vote.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.groupfour.chatapp.chatapp.models;

import javax.persistence.*;
import java.util.Optional;

@Entity
public class Vote {

@Id
@GeneratedValue
@Column(name = "VOTE_ID")
private Long voteId;

@ManyToOne
@JoinColumn(name = "OPTION_ID")
private Option option;

@OneToOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ public interface OptionRepository extends CrudRepository<Option, Long> {





}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.groupfour.chatapp.chatapp.repositories;

import com.groupfour.chatapp.chatapp.models.Option;
import com.groupfour.chatapp.chatapp.models.Poll;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
Expand All @@ -15,6 +16,4 @@ public interface PollRepository extends CrudRepository<Poll, Long> {
Poll findPollByChatAndPoll(Long id,Long pollId);

Iterable<Poll> findPollsByChatChatId(Long id);


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface VoteRepository extends CrudRepository<Vote, Long> {
@Query(value = "SELECT v.* " +
"FROM Option o, Vote v " +
"WHERE o.POLL_ID = ?1 " +
"AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
public Iterable<Vote> findVotesByPoll(Long pollId);

public Integer countVotesByOption_OptionId(Long optionId);
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
import com.groupfour.chatapp.chatapp.repositories.ChatRepository;
import com.groupfour.chatapp.chatapp.repositories.OptionRepository;
import com.groupfour.chatapp.chatapp.repositories.PollRepository;
import com.groupfour.chatapp.chatapp.repositories.VoteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class PollService {

private PollRepository pollRepository;

private VoteRepository voteRepository;
private OptionRepository optionRepository;

@Autowired
public PollService(PollRepository pollRepository) {
public PollService(PollRepository pollRepository, VoteRepository voteRepository, OptionRepository optionRepository) {
this.pollRepository = pollRepository;

this.voteRepository = voteRepository;
this.optionRepository = optionRepository;
}


Expand Down Expand Up @@ -50,6 +54,15 @@ public Poll create(Poll poll) {
return newPoll;
}

public HashMap<String, Integer> getPollVotes(Long pollId) {
Poll poll = pollRepository.findById(pollId).get();
HashMap<String, Integer> map = new HashMap<>();
for (Option option : poll.getOptions()) {
map.put(option.getOptionName(), voteRepository.countVotesByOption_OptionId(option.getOptionId()));
}
return map;
}

public Poll addOptionToPoll(Long pollId, Long optionId) {
Poll poll = getPollById(pollId);
Option option = optionRepository.findById(optionId).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.groupfour.chatapp.chatapp.models.Chat;
import com.groupfour.chatapp.chatapp.models.Poll;
import com.groupfour.chatapp.chatapp.models.Vote;
import com.groupfour.chatapp.chatapp.repositories.ChatRepository;
import com.groupfour.chatapp.chatapp.repositories.PollRepository;
import com.groupfour.chatapp.chatapp.repositories.VoteRepository;
import com.groupfour.chatapp.chatapp.repositories.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.http.ResponseEntity;
Expand All @@ -19,18 +17,25 @@ public class VoteService {
private final PollRepository pollRepository;
private VoteRepository voteRepository;
private ChatRepository chatRepository;
private UserRepository userRepository;
private OptionRepository optionRepository;

@Autowired
public VoteService(PollRepository pollRepository, VoteRepository voteRepository, ChatRepository chatRepository) {
public VoteService(PollRepository pollRepository, VoteRepository voteRepository, ChatRepository chatRepository, UserRepository userRepository, OptionRepository optionRepository) {
this.pollRepository = pollRepository;
this.voteRepository = voteRepository;
this.chatRepository = chatRepository;
this.userRepository = userRepository;
this.optionRepository = optionRepository;
}


public Vote create(Long chatId, Long pollId, Vote vote) {
public Vote create(Long chatId, Long pollId, Long userId, Long optionId) {
Chat chat = chatRepository.findById(chatId).get();
Poll poll = pollRepository.findById(pollId).get();
Vote vote = new Vote();
vote.setVoter(userRepository.findByUserId(userId).get());
vote.setOption(optionRepository.findById(optionId).get());
vote = voteRepository.save(vote);

Set<Vote> votes = poll.getVotes();
Expand Down