Created
April 23, 2021 19:21
-
-
Save sean-brydon/900788aa8b57c409ef1d7249ff2c7cf6 to your computer and use it in GitHub Desktop.
Comments Example
Comments Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package CMP330.Database; | |
import CMP330.Models.User; | |
import CMP330.Utils.DateFns; | |
import com.j256.ormlite.dao.Dao; | |
import com.j256.ormlite.support.ConnectionSource; | |
import java.sql.SQLException; | |
import java.text.DateFormat; | |
public class UserService { | |
private Database db; | |
private DateFns date; | |
public UserService(Database database, DateFns date) { | |
this.db = database; | |
this.date = date; | |
} | |
// TODO: Add a way to check if username exists in db. | |
private User createUser(String username, String password, String email) throws SQLException { | |
// Get the current time as a string | |
String currentTime = date.customDateFormat(DateFns.DateFormatOptions.Default); | |
// Options to create new user | |
User newUser = new User(currentTime, currentTime, username, email, password, "USER"); | |
// Save user to db. | |
db.getUserDao().create(newUser); | |
return newUser; | |
} | |
// Login the user | |
private User loginUser(String username, String password) throws Exception { | |
// Find user by username | |
User foundUser = db.getUserDao().queryForId(username); | |
// Throw error if password is incorrect | |
if(!comparePassword(foundUser,password)) throw new Exception("Invalid Username or Password"); | |
return foundUser; | |
} | |
private Boolean comparePassword(User user, String password){ | |
// Guard clause to check if the user password is equal to the entered password | |
if (user.getPassword().equals(password)) return true; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment