-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/com/skiwi/githubhooksechatservice/dao/RoleDAO.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.skiwi.githubhooksechatservice.dao; | ||
|
||
import com.skiwi.githubhooksechatservice.model.Role; | ||
|
||
public interface RoleDAO { | ||
|
||
Role getRole(int id); | ||
|
||
Role createOrGetRole(String string); | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/skiwi/githubhooksechatservice/dao/RoleDAOImpl.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.skiwi.githubhooksechatservice.dao; | ||
|
||
import org.hibernate.Query; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.skiwi.githubhooksechatservice.model.Role; | ||
|
||
@Repository | ||
public class RoleDAOImpl implements RoleDAO { | ||
|
||
@Autowired | ||
private SessionFactory sessionFactory; | ||
|
||
private Session openSession() { | ||
return sessionFactory.getCurrentSession(); | ||
} | ||
|
||
@Override | ||
public Role getRole(int id) { | ||
Role role = (Role) openSession().load(Role.class, id); | ||
return role; | ||
} | ||
|
||
@Override | ||
public Role createOrGetRole(String roleName) { | ||
try { | ||
Query query = openSession().createQuery("from Role r where r.role = :role"); | ||
query.setParameter("role", roleName); | ||
System.out.println("executing role query with parameter " + roleName); | ||
Role role = (Role) query.uniqueResult(); | ||
if (role != null) { | ||
return role; | ||
} | ||
role = new Role(); | ||
role.setRole(roleName); | ||
openSession().save(role); | ||
return role; | ||
} | ||
catch (Exception ex) { | ||
System.out.println("BIG FAT ERROR: " + ex); | ||
ex.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/skiwi/githubhooksechatservice/dao/UserDAO.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.skiwi.githubhooksechatservice.dao; | ||
|
||
import com.skiwi.githubhooksechatservice.model.DugaUser; | ||
|
||
public interface UserDAO { | ||
|
||
public DugaUser getUser(String login); | ||
|
||
DugaUser createUser(String login, String password); | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/skiwi/githubhooksechatservice/dao/UserDAOImpl.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.skiwi.githubhooksechatservice.dao; | ||
|
||
import org.hibernate.Query; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.skiwi.githubhooksechatservice.model.DugaUser; | ||
|
||
@Repository | ||
@Transactional | ||
public class UserDAOImpl implements UserDAO { | ||
|
||
@Autowired | ||
private SessionFactory sessionFactory; | ||
|
||
@Autowired | ||
private RoleDAO roleDAO; | ||
|
||
@Autowired | ||
private PasswordEncoder passwordEncoder; | ||
|
||
private Session openSession() { | ||
return sessionFactory.getCurrentSession(); | ||
} | ||
|
||
@Override | ||
public DugaUser getUser(String login) { | ||
try { | ||
Query query = openSession().createQuery("from User u where u.login = :login"); | ||
query.setParameter("login", login); | ||
System.out.println("executing query with parameter" + login); | ||
return (DugaUser) query.uniqueResult(); | ||
} | ||
catch (Exception ex) { | ||
System.out.println("BIG FAT ERROR: " + ex); | ||
ex.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public DugaUser createUser(String login, String password) { | ||
DugaUser user = getUser(login); | ||
if (user != null) { | ||
return null; | ||
} | ||
|
||
try { | ||
user = new DugaUser(); | ||
user.setLogin(login); | ||
user.setPassword(passwordEncoder.encode(password)); | ||
user.setRole(roleDAO.createOrGetRole("MODERATOR")); | ||
openSession().save(user); | ||
return user; | ||
} | ||
catch (Exception ex) { | ||
System.out.println("BIG FAT ERROR: " + ex); | ||
ex.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
} |