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
10 changes: 10 additions & 0 deletions src/main/java/com/example/demo/controller/ChannelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public ResponseEntity<Channel> readChannel(@PathVariable Long id) {
return new ResponseEntity<>(service.readChannel(id),HttpStatus.NOT_FOUND);
}

@GetMapping("/exists/{name}")
public ResponseEntity<Boolean> existsByName(@PathVariable String name) {
return new ResponseEntity<>(service.existsByName(name), HttpStatus.OK);
}

@GetMapping("find/{name}")
public ResponseEntity<Channel> findByName(@PathVariable String name) {
return new ResponseEntity<>(service.findByName(name), HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.GET, value = "/readAll")
public ResponseEntity<List<Channel>> readAllChannels() {
return new ResponseEntity<>(service.readAllChannels(), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public ResponseEntity<Message> create(@RequestBody Message message) {
return new ResponseEntity<>(messageService.create(message), HttpStatus.CREATED);
}

@GetMapping("/find/{channelName}")
public ResponseEntity<List<Message>> findByChannelName(@PathVariable String channelName) {
return new ResponseEntity<>(messageService.findByChannelName(channelName), HttpStatus.OK);
}

@GetMapping(value = "/read/{id}")
public ResponseEntity<Message> read(@PathVariable Long id ){
return new ResponseEntity<>(messageService.read(id), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public class ProfileController {
@PostMapping(value = "/register")
public ResponseEntity<?> createProfile(@RequestBody Profile profile) {
if (service.existsByUsername(profile.getUsername())) {
return ResponseEntity.badRequest().body("Username is taken");
return new ResponseEntity<>("Username is taken", HttpStatus.OK);
}
if (service.existsByEmail(profile.getEmail())) {
return ResponseEntity.badRequest().body("Email is taken");
return new ResponseEntity<>("Email is taken", HttpStatus.OK);
}
profile.setPassword(passwordEncoder.encode(profile.getPassword()));
return new ResponseEntity<>(service.createProfile(profile), HttpStatus.CREATED);
Expand Down
56 changes: 34 additions & 22 deletions src/main/java/com/example/demo/models/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,32 @@ public class Channel {
private String name;
@Enumerated(value = EnumType.STRING)
private ChannelType type;
@ManyToMany(fetch = FetchType.LAZY)
private List<Profile> profileList;
@OneToMany(fetch = FetchType.LAZY)
private List<Message> messages;
// @ManyToMany(mappedBy = "channels", fetch = FetchType.LAZY)
// private List<Profile> profileList;
// @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
// private List<Message> messages;

public Channel() {
}

public Channel(Long id, String name, ChannelType type, List<Profile> profileList, List<Message> messages) {
// public Channel(Long id, String name, ChannelType type, List<Profile> profileList, List<Message> messages) {
// this.id = id;
// this.name = name;
// this.type = type;
// this.profileList = profileList;
// this.messages = messages;
// }


public Channel(Long id, String name, ChannelType type) {
this.id = id;
this.name = name;
this.type = type;
this.profileList = profileList;
this.messages = messages;
}

public Channel(String name, ChannelType type) {
this.name = name;
this.type = type;
}

public Long getId() {
Expand All @@ -48,21 +60,21 @@ public void setName(String name) {
this.name = name;
}

public List<Profile> getProfileList() {
return profileList;
}

public void setProfileList(List<Profile> profileList) {
this.profileList = profileList;
}

public List<Message> getMessages() {
return messages;
}

public void setMessages(List<Message> messages) {
this.messages = messages;
}
// public List<Profile> getProfileList() {
// return profileList;
// }
//
// public void setProfileList(List<Profile> profileList) {
// this.profileList = profileList;
// }
//
// public List<Message> getMessages() {
// return messages;
// }
//
// public void setMessages(List<Message> messages) {
// this.messages = messages;
// }

public ChannelType getType() {
return type;
Expand Down
38 changes: 20 additions & 18 deletions src/main/java/com/example/demo/models/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,27 @@ public class Message {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "profile_id", referencedColumnName = "id")
Profile profile;
// @ManyToOne(fetch = FetchType.LAZY)
// @JoinColumn(name = "profile_id", referencedColumnName = "id")
// Profile profile;
String profileSentFrom;
String body;
String timestamp;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "channel_id", referencedColumnName = "id")
Channel channel;
String timestamp = new Date().toString();
String channelName;
// @ManyToOne(fetch = FetchType.LAZY)
// @JoinColumn(name = "channel_id", referencedColumnName = "id")
// Channel channel;


public Message() {
}

public Message(Long id, Profile profile, String body, String timestamp, Channel channel) {
public Message(Long id, String profileSentFrom, String body, String timestamp, String channelName) {
this.id = id;
this.profile = profile;
this.profileSentFrom = profileSentFrom;
this.body = body;
this.timestamp = timestamp;
this.channel = channel;
this.channelName = channelName;
}

public Long getId() {
Expand All @@ -58,19 +60,19 @@ public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}

public Profile getProfile() {
return profile;
public String getProfileSentFrom() {
return profileSentFrom;
}

public void setProfile(Profile profile) {
this.profile = profile;
public void setProfileSentFrom(String profileSentFrom) {
this.profileSentFrom = profileSentFrom;
}

public Channel getChannel() {
return channel;
public String getChannelName() {
return channelName;
}

public void setChannel(Channel channel) {
this.channel = channel;
public void setChannelName(String channelName) {
this.channelName = channelName;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/example/demo/models/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public class Profile implements UserDetails {
private String password;
private String email;
private boolean enabled = true;
// @ManyToMany(cascade = CascadeType.ALL)
// @ManyToMany(fetch = FetchType.LAZY)
// @JoinTable(joinColumns = @JoinColumn(name = "profile_id"),
// inverseJoinColumns = @JoinColumn(name = "channel_id"))
// private List<Channel> channels;
// @OneToMany(mappedBy = "profile")
// @OneToMany(mappedBy = "profile", cascade = CascadeType.ALL)
// private List<Message> messages;

public Profile() {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/example/demo/repository/ChannelRepo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.example.demo.repository;

import com.example.demo.models.Channel;
import com.example.demo.models.ChannelType;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ChannelRepo extends JpaRepository<Channel, Long> {
List<Channel> findByType(ChannelType type);

boolean existsByName(String name);

Channel findByName(String name);
}
6 changes: 3 additions & 3 deletions src/main/java/com/example/demo/repository/MessageRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MessageRepo extends JpaRepository<Message, Long> {


Iterable<Message> getMessageByChannel_Id(Long id);
List<Message> findByChannelName(String channelName);

}
41 changes: 27 additions & 14 deletions src/main/java/com/example/demo/service/ChannelService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.demo.service;

import com.example.demo.models.Channel;
import com.example.demo.models.ChannelType;
import com.example.demo.models.Profile;
import com.example.demo.repository.ChannelRepo;
import com.example.demo.repository.ProfileRepo;
Expand Down Expand Up @@ -38,23 +39,35 @@ public List<Channel> readAllChannels() {
return result;
}

public List<Channel> findByProfileUsername(String username) {
List<Channel> allChannels = readAllChannels();
return allChannels
.stream()
.filter(channel -> {
List<Profile> profilesInChannel = channel.getProfileList();
for (Profile profile : profilesInChannel) {
if (profile.getUsername().equals(username)) {
return true;
}
}
return false;
})
.collect(Collectors.toList());
public List<Channel> readAllChannelsByType(ChannelType type) {
return channelRepo.findByType(type);
}

public boolean existsByName(String name) {
return channelRepo.existsByName(name);
}

public Channel findByName(String name) {
return channelRepo.findByName(name);
}

// public List<Channel> findByProfileUsername(String username) {
// List<Channel> allChannels = readAllChannels();
// return allChannels
// .stream()
// .filter(channel -> {
// List<Profile> profilesInChannel = channel.getProfileList();
// for (Profile profile : profilesInChannel) {
// if (profile.getUsername().equals(username)) {
// return true;
// }
// }
// return false;
// })
// .collect(Collectors.toList());
//
// }

public Channel update(Long id, Channel channel) {
Channel channelInDb = readChannel(id);
channelInDb.setId(channel.getId());
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/example/demo/service/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public Message create (Message message){
return messageRepo.save(message);
}

public List<Message> findByChannelName(String channelName) {
return messageRepo.findByChannelName(channelName);
}

public Message read(Long id){
return messageRepo.getById(id);
}
Expand All @@ -37,7 +41,8 @@ public List<Message> readAll(){
public Message update(Long id, Message newMessageData){
Message messageInDB = read(id);
messageInDB.setBody(newMessageData.getBody());
messageInDB.setProfile(newMessageData.getProfile());
messageInDB.setProfileSentFrom(newMessageData.getProfileSentFrom());
messageInDB.setChannelName(newMessageData.getChannelName());
messageInDB.setTimestamp(newMessageData.getTimestamp());
messageRepo.save(messageInDB);
return messageInDB;
Expand Down
Binary file modified target/classes/com/example/demo/controller/ChannelController.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/controller/MessageController.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/controller/ProfileController.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/models/Channel.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/models/Message.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/repository/ChannelRepo.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/repository/MessageRepo.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/service/ChannelService.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/service/MessageService.class
Binary file not shown.