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

From bhargavi branch to integration branch #3

Open
wants to merge 4 commits into
base: IntegrationDemo
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/accolite/survey/DAO/README.md

This file was deleted.

59 changes: 59 additions & 0 deletions src/main/java/com/accolite/survey/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.accolite.survey.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.accolite.survey.entity.User;
import com.accolite.survey.service.UserService;

@RestController
@RequestMapping("/api/user")
public class UserController {

private final UserService userService;

@Autowired
public UserController(UserService userService) {
this.userService = userService;
}

@PostMapping
public ResponseEntity addUser(@RequestBody User user) {
userService.addUser(user);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@PutMapping
public ResponseEntity updateUser(@RequestBody User user) {
userService.updateUser(user);
return ResponseEntity.ok().build();
}

@GetMapping
public ResponseEntity<List<User>> getAllUsers() {
return ResponseEntity.ok(userService.getAllUsers());
}

@GetMapping("/{employeeId}")
public ResponseEntity getUserByEmployeeId(@PathVariable String employeeId) {
return ResponseEntity.ok(userService.getUserByEmployeeId(employeeId));
}

@DeleteMapping("/{id}")
public ResponseEntity deleteUser(@PathVariable String id) {
userService.deleteUser(id);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/accolite/survey/dao/UserDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.accolite.survey.dao;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;

import com.accolite.survey.entity.User;

@Repository
public interface UserDAO extends MongoRepository<User, String>{

}
77 changes: 77 additions & 0 deletions src/main/java/com/accolite/survey/entity/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.accolite.survey.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

@Document("user")
public class User {

@Id
@Field(name = "employeeId")
private String employeeId;

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

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

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

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

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

public User(String employeeId, String password, String name, String email, String role, String dateOfJoining) {
this.employeeId = employeeId;
this.password = password;
this.name = name;
this.email = email;
this.role = role;
this.dateOfJoining = dateOfJoining;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getDateOfJoining() {
return dateOfJoining;
}
public void setDateOfJoining(String dateOfJoining) {
this.dateOfJoining = dateOfJoining;
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/accolite/survey/service/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.accolite.survey.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.accolite.survey.dao.UserDAO;
import com.accolite.survey.entity.User;

@Service
public class UserService {

private final UserDAO userDAO;

@Autowired
public UserService(UserDAO userDAO) {
this.userDAO = userDAO;
}

public void addUser(User user) {
userDAO.insert(user);
}

public void updateUser(User user) {
User savedUser = userDAO.findById(user.getEmployeeId()).orElseThrow(
() -> new RuntimeException(String.format("Cannot find User by Id %s", user.getEmployeeId())));

savedUser.setName(user.getName());
savedUser.setPassword(user.getPassword());
savedUser.setRole(user.getRole());
savedUser.setDateOfJoining(user.getDateOfJoining());
savedUser.setEmail(user.getEmail());

userDAO.save(user);

}

public List<User> getAllUsers() {
return userDAO.findAll();
}

public User getUserByEmployeeId(String employeeId) {
return userDAO.findById(employeeId).orElseThrow(
() -> new RuntimeException(String.format("Cannot find User with EmplyeeId %s", employeeId)));
}

public void deleteUser(String id) {
userDAO.deleteById(id);
}

public User findByEmployeeIdAndPassword(String employeeId, String password) {

User user = userDAO.findById(employeeId).orElseThrow(
() -> new RuntimeException(String.format("Cannot find User with EmplyeeId %s", employeeId)));
System.out.println(user.getPassword()+" : "+password);
if((user.getPassword()).equals(password))
return user;
else
throw new RuntimeException(String.format("Incorrect password"));
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
spring.data.mongodb.uri=mongodb://localhost:27017/Survey
spring.data.mongodb.uri=mongodb: