Skip to content
Snippets Groups Projects
Commit 7ff5c070 authored by dmz39's avatar dmz39
Browse files

Updating User Accounts functionality that was not previously pushed

parent 811e231a
Branches
No related tags found
No related merge requests found
package edu.drexel.TrainDemo.Services; package edu.drexel.TrainDemo.Services;
import edu.drexel.TrainDemo.models.User; import edu.drexel.TrainDemo.models.UserEntity;
import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -8,11 +8,11 @@ import java.util.List; ...@@ -8,11 +8,11 @@ import java.util.List;
@Service @Service
public interface UserService { public interface UserService {
List<User> getUsers(); List<UserEntity> getUsers();
User getUser(OAuth2User principal); UserEntity getUser(OAuth2User principal);
User getUser(long id); UserEntity getUser(long id);
User createUser(long id); UserEntity createUser(long id, String firstName, String lastName);
void saveUser(OAuth2User principal, User newUser); void saveUser(OAuth2User principal, UserEntity newUser);
void saveUser(User user, User newUser); void saveUser(UserEntity user, UserEntity newUser);
void removeUser(OAuth2User principal); void removeUser(OAuth2User principal);
} }
package edu.drexel.TrainDemo.Services; package edu.drexel.TrainDemo.Services;
import edu.drexel.TrainDemo.models.User; import edu.drexel.TrainDemo.Utils;
import edu.drexel.TrainDemo.models.UserEntity;
import edu.drexel.TrainDemo.repositories.UserRepository; import edu.drexel.TrainDemo.repositories.UserRepository;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Service
public class UserServiceImpl implements UserService { public class UserServiceImpl implements UserService {
@Autowired @Autowired
...@@ -18,43 +21,49 @@ public class UserServiceImpl implements UserService { ...@@ -18,43 +21,49 @@ public class UserServiceImpl implements UserService {
private UserRepository userRepo; private UserRepository userRepo;
@Override @Override
public List<User> getUsers() { public List<UserEntity> getUsers() {
List<User> allUsers = new ArrayList<>(); List<UserEntity> allUsers = new ArrayList<>();
this.userRepo.findAll().forEach(allUsers::add); this.userRepo.findAll().forEach(allUsers::add);
return allUsers; return allUsers;
} }
@Override @Override
public User getUser(OAuth2User principal) { public UserEntity getUser(OAuth2User principal) {
long id = principal.getAttribute("id"); long id = Utils.intToLong(principal.getAttribute("id"));
return getUser(id); return getUser(id);
} }
@Override @Override
public User getUser(long id) { public UserEntity getUser(long id) {
User user = this.userRepo.findById(id); UserEntity user = this.userRepo.findByExternalId(id);
// TODO: If user does not exist, then create the user. // TODO: If user does not exist, then create the user. Possibly
if (user == null) { if (user == null) {
user = createUser(id); //user = createUser(id, "x", "x");
} else {
System.out.println("getUser: " + user.getExternalId() + " | " + user.getFirstName() + " | " + user.getLastName());
} }
return user; return user;
} }
@Override @Override
public User createUser(long id) { public UserEntity createUser(long id, String firstName, String lastName) {
return new User("John", "Doe", id); UserEntity newUser = new UserEntity(firstName, lastName, id);
System.out.println("createUser: " + newUser.getExternalId() + " | " + newUser.getFirstName() + " | " + newUser.getLastName());
this.userRepo.save(newUser);
getUser(id);
return newUser;
} }
@Override @Override
public void saveUser(OAuth2User principal, User newUser) { public void saveUser(OAuth2User principal, UserEntity newUser) {
User oldUser = getUser(principal); UserEntity oldUser = getUser(principal);
saveUser(oldUser, newUser); saveUser(oldUser, newUser);
} }
@Override @Override
public void saveUser(User oldUser, User newUser) { public void saveUser(UserEntity oldUser, UserEntity newUser) {
// TODO: Implement this // TODO: Implement this
} }
... ...
......
...@@ -3,6 +3,6 @@ package edu.drexel.TrainDemo; ...@@ -3,6 +3,6 @@ package edu.drexel.TrainDemo;
public class Utils { public class Utils {
public static long intToLong(int input) { public static long intToLong(int input) {
return ((Number) ) return ((Number) (input)).longValue();
} }
} }
...@@ -48,9 +48,9 @@ public class UserController { ...@@ -48,9 +48,9 @@ public class UserController {
} }
@RequestMapping("/user/new") @RequestMapping("/user/new")
String newUser(@AuthenticationPrincipal OAuth2User principal, @RequestParam String firstName, @RequestParam String lastName) { RedirectView newUser(@AuthenticationPrincipal OAuth2User principal, @RequestParam String firstName, @RequestParam String lastName) {
System.out.println(principal.getAttribute("id") + " | " + firstName + " | " + lastName); System.out.println(principal.getAttribute("id") + " | " + firstName + " | " + lastName);
this.userService.createUser(Utils.intToLong(principal.getAttribute("id")), firstName, lastName); this.userService.createUser(Utils.intToLong(principal.getAttribute("id")), firstName, lastName);
return "index"; return new RedirectView("/");
} }
} }
...@@ -10,24 +10,31 @@ public class UserEntity implements Serializable { ...@@ -10,24 +10,31 @@ public class UserEntity implements Serializable {
static final long serialVersionUID = 42L; static final long serialVersionUID = 42L;
@Id @Id
@GeneratedValue(strategy=GenerationType.AUTO) @GeneratedValue(strategy=GenerationType.IDENTITY)
private long id; private Long id;
@Column(name = "firstName")
@Column(name = "first_name")
private String firstName; private String firstName;
@Column(name = "lastName")
@Column(name = "last_name")
private String lastName; private String lastName;
@Column(name = "isAdmin")
private boolean isAdmin;
@Column(name = "is_admin")
private Boolean isAdmin;
@Column(name = "external_id")
private Long externalId;
public UserEntity(){}
public UserEntity(String firstName, String lastName, long clientid) { public UserEntity(String firstName, String lastName, long clientid) {
this.firstName = firstName; this.firstName = firstName;
this.lastName = lastName; this.lastName = lastName;
this.id = clientid; this.externalId = clientid;
this.isAdmin = false; this.isAdmin = false;
} }
public long getId() { public Long getId() {
return this.id; return this.id;
} }
...@@ -43,11 +50,15 @@ public class UserEntity implements Serializable { ...@@ -43,11 +50,15 @@ public class UserEntity implements Serializable {
return this.isAdmin; return this.isAdmin;
} }
public Long getExternalId() {
return this.externalId;
}
/** /**
* This function is most likely only used during development and eval for looking at admin panels. * This function is most likely only used during development and eval for looking at admin panels.
* @param shouldBeAdmin * @param shouldBeAdmin
*/ */
public void setIsAdmin(boolean shouldBeAdmin) { public void setIsAdmin(Boolean shouldBeAdmin) {
this.isAdmin = shouldBeAdmin; this.isAdmin = shouldBeAdmin;
} }
... ...
......
package edu.drexel.TrainDemo.repositories; package edu.drexel.TrainDemo.repositories;
import edu.drexel.TrainDemo.models.User; import edu.drexel.TrainDemo.models.UserEntity;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import java.util.List; import java.util.List;
public interface UserRepository extends CrudRepository<User, Long> { public interface UserRepository extends CrudRepository<UserEntity, Long> {
List<User> findByLastName(String lastName); List<UserEntity> findByLastName(String lastName);
User findById(long id); UserEntity findById(long id);
UserEntity findByExternalId(long externalId);
} }
...@@ -14,6 +14,9 @@ $.ajaxSetup({ ...@@ -14,6 +14,9 @@ $.ajaxSetup({
$.get("/user", function (data) { $.get("/user", function (data) {
$("#profile-name").html(data.name); $("#profile-name").html(data.name);
if (data.name == null) {
$("#newAccountModal").modal("show");
}
$(".unauthenticated").hide(); $(".unauthenticated").hide();
$(".authenticated").show(); $(".authenticated").show();
}).fail(function () { }).fail(function () {
... ...
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.1/js.cookie.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.1/js.cookie.js"></script>
<script type="text/javascript" src="/js/script.js"></script> <script type="text/javascript" src="/js/script.js"></script>
<script type="text/javascript" src="/js/user.js"></script>
</th:block> </th:block>
</head> </head>
<body> <body>
...@@ -73,6 +74,34 @@ ...@@ -73,6 +74,34 @@
</div> </div>
</div> </div>
</div> </div>
<div class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" id="newAccountModal">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newAccountModalLongTitle">New Account</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p>It seems you are a new user on the site. We need this bit of information about you as a passenger.</p>
<form>
<div class="form-group">
<label for="fname">First Name:</label>
<input type="text" class="form-control" id="fname">
</div>
<div class="form-group">
<label for="lname">Last Name:</label>
<input type="text" class="form-control" id="lname">
</div>
<div class="form-group">
<button onclick="addUser()">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
</th:block> </th:block>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -32,15 +32,6 @@ ...@@ -32,15 +32,6 @@
</div> </div>
</div> </div>
<div class="row list-row"> <div class="row list-row">
</div>
</div>
</div>
</div>
</main>
</body>
</html>
<div class="container address-container"> <div class="container address-container">
<form> <form>
<a>Enter New Billing Info:</a> <a>Enter New Billing Info:</a>
...@@ -76,3 +67,10 @@ ...@@ -76,3 +67,10 @@
</div> </div>
</form> </form>
</div> </div>
</div>
</div>
</div>
</div>
</main>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment