Skip to content

Issue 6: Response to users' page requests #10

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot', version: springBootVersion
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBootVersion
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb', version: springBootVersion
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth', version: springSleuthVersion
compile group: 'javax.el', name: 'javax.el-api', version: javaxElApiVersion
compile group: 'org.glassfish', name: 'javax.el', version: glassfishVersion
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: javaxServletVersion
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
compile 'org.mongodb:mongo-java-driver:3.12.7'
compile 'org.mongodb:mongodb-driver-core:3.12.7'
compile 'commons-cli:commons-cli:1.4'
compile 'com.google.guava:guava:30.1-jre'
compile 'org.mongodb:bson:4.1.1'
compile 'com.googlecode.json-simple:json-simple:1.1.1'
}

task publishAllJars() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/TechVault/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@SpringBootApplication
@SpringBootApplication(scanBasePackages="TechVault")
@EnableMongoRepositories(basePackages = "TechVault")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/TechVault/controller/ContentInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package TechVault.controller;

public class ContentInterface {
}
13 changes: 13 additions & 0 deletions src/main/java/TechVault/controller/HomeInterface.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package TechVault.controller;

import TechVault.service.user.UserService;
import TechVault.service.user.response.ContentResponse;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@Validated
@RestController
@RequestMapping("home")
public class HomeInterface {

@Autowired
private UserService userService;

/**
* To get the home page sorted by likes in descending order.
* @return A Response entity which will have all the blogs to be loaded as home page.
Expand Down Expand Up @@ -46,4 +58,5 @@ public ResponseEntity<?> getLatest() {
public ResponseEntity<?> getTrending() {
return null;
}

}
41 changes: 39 additions & 2 deletions src/main/java/TechVault/controller/UserInterface.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package TechVault.controller;

import TechVault.service.user.UserService;
import TechVault.service.user.request.LoginRequest;
import TechVault.service.user.request.UserProfileCreateRequest;
import TechVault.service.user.response.ContentResponse;
import TechVault.service.user.response.LoginResponse;
import TechVault.service.user.response.UserProfileCreateResponse;
import TechVault.service.user.response.UserProfileResponse;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -11,12 +24,36 @@
@RequestMapping("user")
public class UserInterface {

@Autowired
private UserService userService;

@RequestMapping(method = RequestMethod.GET, value = "/{contentID}")
public ResponseEntity<?> getContent(@PathVariable String contentID) {
System.out.println("Entering get Content");
ContentResponse contentResponse = userService.getContent(contentID);
System.out.println("Content Response = " + contentResponse.getTitle());
return new ResponseEntity<>(contentResponse, HttpStatus.OK);
}

/**
* To the the profile of the user corresponding to the user id.
* @return A Response entity which will have all the user details.
*/
@RequestMapping(method = RequestMethod.GET, value = "/{user_id}")
public ResponseEntity<?> getUserProfile() {
return null;
public ResponseEntity<?> getUserProfile(@PathVariable String userID) {
UserProfileResponse userProfileResponse = userService.userProfile(userID);
return new ResponseEntity<>(userProfileResponse, userProfileResponse.getStatus());
}

@RequestMapping(method = RequestMethod.POST, value = "/createProfile", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> createUserProfile(@RequestBody UserProfileCreateRequest userProfileRequest) {
UserProfileCreateResponse userProfileCreateResponse = userService.createUserProfile(userProfileRequest);
return new ResponseEntity<>(userProfileCreateResponse, userProfileCreateResponse.getStatus());
}

@RequestMapping(method = RequestMethod.POST, value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> login(@RequestBody LoginRequest loginRequest) {
LoginResponse loginResponse = userService.login(loginRequest);
return new ResponseEntity<>(loginResponse, loginResponse.getStatus());
}
}
20 changes: 20 additions & 0 deletions src/main/java/TechVault/service/user/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package TechVault.service.user;

import TechVault.service.user.request.LoginRequest;
import TechVault.service.user.request.UserProfileCreateRequest;
import TechVault.service.user.response.ContentResponse;
import TechVault.service.user.response.LoginResponse;
import TechVault.service.user.response.UserProfileCreateResponse;
import TechVault.service.user.response.UserProfileResponse;

import java.util.UUID;

public interface UserService {
public ContentResponse getContent(String contentID);

public UserProfileCreateResponse createUserProfile(UserProfileCreateRequest userProfileCreateRequest);

public LoginResponse login(LoginRequest loginRequest);

public UserProfileResponse userProfile(String userId);
}
82 changes: 82 additions & 0 deletions src/main/java/TechVault/service/user/UserServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package TechVault.service.user;

import TechVault.service.user.model.BookMark;
import TechVault.service.user.model.Content;
import TechVault.service.user.model.User;
import TechVault.service.user.persistence.BookMarkDao;
import TechVault.service.user.persistence.ContentDao;
import TechVault.service.user.persistence.UserDao;
import TechVault.service.user.request.LoginRequest;
import TechVault.service.user.request.UserProfileCreateRequest;
import TechVault.service.user.response.ContentResponse;
import TechVault.service.user.response.LoginResponse;
import TechVault.service.user.response.UserProfileCreateResponse;
import TechVault.service.user.response.UserProfileResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{

@Autowired
private ContentDao contentDao;

@Autowired
private UserDao userDao;

@Autowired
private BookMarkDao bookMarkDao;

@Override
public ContentResponse getContent(String contentID) {
Content content = contentDao.getContentByID(contentID);
System.out.println("Content Response = " + content.getTitle());
return new ContentResponse(content.getTitle(), content.getCompany(), content.getDatePosted());
}

@Override
public UserProfileCreateResponse createUserProfile(UserProfileCreateRequest userProfileCreateRequest) {
try {
userDao.addUser(userProfileCreateRequest.getEmail(), userProfileCreateRequest.getPassword(), userProfileCreateRequest.getUserName());
return new UserProfileCreateResponse("Created profile.", HttpStatus.CREATED);
} catch (Exception e) {
return new UserProfileCreateResponse("Failed to create profile.", HttpStatus.BAD_REQUEST);
}
}

@Override
public LoginResponse login(LoginRequest loginRequest) {
if (userDao.getUserByEmailPassword(loginRequest.getEmail(), loginRequest.getPassword())) {
return new LoginResponse("Successful", HttpStatus.OK);
}
return new LoginResponse("Try again!", HttpStatus.BAD_REQUEST);
}

@Override
public UserProfileResponse userProfile(String userId) {
try {
User user = userDao.getUserByUserId(userId);

List<BookMark> bookMarks = bookMarkDao.getBookMarksByUserId(userId);

List<ContentResponse> contentResponseList = new ArrayList<>();

for (BookMark bookMark : bookMarks) {
String contentId = bookMark.getContentId();

Content content = contentDao.getContentByID(contentId);

contentResponseList.add(new ContentResponse(content.getTitle(), content.getCompany(), bookMark.getPostedTime().toString()));
}

return new UserProfileResponse(userId, contentResponseList, HttpStatus.OK);
} catch (Exception e) {
return new UserProfileResponse(null, null, HttpStatus.BAD_REQUEST);
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/TechVault/service/user/model/BookMark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package TechVault.service.user.model;

import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.security.Timestamp;

@Document(collection = "bookmark")
@Builder
@Getter
@Data
@Setter
public class BookMark {
@Field(name = "contentId")
private String contentId;

@Field(name = "userId")
private String userId;

@Field(name = "timeStamp")
private Timestamp postedTime;
}
57 changes: 57 additions & 0 deletions src/main/java/TechVault/service/user/model/Content.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package TechVault.service.user.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Document(collection = "blogs")
@Builder
@Getter
@Data
@Setter
public class Content {
@Field(name = "uuid")
private String uuid;

@Field(name = "date")
private String datePosted;

@Field(name = "author")
private String author;

@Field(name = "link")
private String link;

@Field(name = "company")
private String company;

@Field(name = "abstract")
private String abstractText;

@Field(name = "categories")
private String categories;

@Field(name = "title")
private String title;

public String getTitle() {
return title;
}

public String getCompany() {
return company;
}

public String getDatePosted() {
return datePosted;
}
}
27 changes: 27 additions & 0 deletions src/main/java/TechVault/service/user/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package TechVault.service.user.model;

import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

@Document(collection = "user")
@Builder
@Getter
@Data
@Setter
public class User {
@Field(name = "userId")
private String userId;

@Field(name = "userName")
private String userName;

@Field(name = "email")
private String email;

@Field(name = "password")
private String password;
}
10 changes: 10 additions & 0 deletions src/main/java/TechVault/service/user/persistence/BookMarkDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package TechVault.service.user.persistence;


import TechVault.service.user.model.BookMark;

import java.util.List;

public interface BookMarkDao {
public List<BookMark> getBookMarksByUserId(String userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package TechVault.service.user.persistence;

import TechVault.service.user.model.BookMark;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class BookMarkDaoImpl implements BookMarkDao {

@Autowired
private BookMarkRepository bookMarkRepository;

@Override
public List<BookMark> getBookMarksByUserId(String userId) {
return this.bookMarkRepository.findAllByUserId(userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package TechVault.service.user.persistence;

import TechVault.service.user.model.BookMark;
import TechVault.service.user.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface BookMarkRepository extends MongoRepository<BookMark, String> {
public List<BookMark> findAllByUserId(String userId);
}
Loading