history: repository, model, controller methods
This commit is contained in:
1 parent
b7f19e5567
commit
6942009358
8 files changed
+247
-87
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package application;
|
||||
|
||||
import application.repositories.DoorRepository;
|
||||
import application.repositories.HistoryEntryRepository;
|
||||
import application.repositories.RoleRepository;
|
||||
import application.repositories.UserRepository;
|
||||
import application.utils.Initializer;
|
||||
@@ -38,10 +39,11 @@ public class Config extends SpringBootServletInitializer {
|
||||
public static void main(String[] args) {
|
||||
|
||||
ConfigurableApplicationContext context = SpringApplication.run(Config.class, args);
|
||||
UserRepository userRepository=context.getBean(UserRepository.class);
|
||||
RoleRepository roleRepository=context.getBean(RoleRepository.class);
|
||||
DoorRepository doorRepository=context.getBean(DoorRepository.class);
|
||||
new Initializer(roleRepository, userRepository, doorRepository).init();
|
||||
UserRepository userRepository = context.getBean(UserRepository.class);
|
||||
RoleRepository roleRepository = context.getBean(RoleRepository.class);
|
||||
DoorRepository doorRepository = context.getBean(DoorRepository.class);
|
||||
HistoryEntryRepository historyEntryRepository = context.getBean(HistoryEntryRepository.class);
|
||||
new Initializer(roleRepository, userRepository, doorRepository, historyEntryRepository).init();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package application;
|
||||
|
||||
/**
|
||||
* Created by Marcin on 2015-01-16.
|
||||
*/
|
||||
public final class Constants {
|
||||
public static final boolean SUCCESS_TRUE = true;
|
||||
public static final boolean SUCCESS_FALSE = false;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package application.controllers;
|
||||
|
||||
import application.Constants;
|
||||
import application.model.HistoryEntry;
|
||||
import application.model.Message;
|
||||
import application.model.Role;
|
||||
import application.model.User;
|
||||
import application.network.response.BaseResponse;
|
||||
import application.repositories.DoorRepository;
|
||||
import application.repositories.HistoryEntryRepository;
|
||||
import application.repositories.RoleRepository;
|
||||
import application.repositories.UserRepository;
|
||||
import org.json.JSONArray;
|
||||
@@ -15,6 +20,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -30,27 +36,36 @@ public class UserController {
|
||||
@Autowired
|
||||
private RoleRepository roleRepository;
|
||||
|
||||
public void setUserRepository(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
@Autowired
|
||||
private
|
||||
HistoryEntryRepository historyEntryRepository;
|
||||
|
||||
public void setRoleRepository(RoleRepository roleRepository) {
|
||||
this.roleRepository = roleRepository;
|
||||
}
|
||||
@Autowired
|
||||
DoorRepository doorRepository;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@RequestMapping(value = "/logUserEntry/{userId}/{doorId}")
|
||||
@ResponseBody
|
||||
public BaseResponse logUserEntry(@PathVariable("userId") long userId, @PathVariable("doorId") long doorId) throws JSONException {
|
||||
HistoryEntry historyEntry = new HistoryEntry();
|
||||
historyEntry.setDate(new Date().getTime());
|
||||
historyEntry.setUser(userRepository.findOne(userId));
|
||||
historyEntry.setDoor(doorRepository.findOne(doorId));
|
||||
historyEntryRepository.save(historyEntry);
|
||||
return new BaseResponse().init(Constants.SUCCESS_TRUE);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/history")
|
||||
@ResponseBody
|
||||
public Iterable<HistoryEntry> getHistory() throws JSONException {
|
||||
return historyEntryRepository.findAll();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/add")
|
||||
@ResponseBody
|
||||
//powinno mapowac obiekt json na obiekt user
|
||||
public String addUser(@RequestBody User user) {
|
||||
/*User user = new User();
|
||||
user.setFirstName(username);
|
||||
Set<Role> roles = new HashSet<Role>();
|
||||
roles.add(new Role("lala","lala"));
|
||||
roles.add(new Role("lala1","lala1"));
|
||||
user.setRoles(roles);*/
|
||||
userRepository.save(user);
|
||||
return "saved user: "+user.getUserId();
|
||||
}
|
||||
@@ -219,4 +234,12 @@ public class UserController {
|
||||
userRepository.delete(userId);
|
||||
return "user deleted: "+userId;
|
||||
}
|
||||
|
||||
public void setUserRepository(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public void setRoleRepository(RoleRepository roleRepository) {
|
||||
this.roleRepository = roleRepository;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package application.model;
|
||||
|
||||
/**
|
||||
* Created by Marcin on 2015-01-31.
|
||||
*/
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
/**
|
||||
* Represents one item on history list.
|
||||
* Contains User, Date and Door ID
|
||||
*/
|
||||
|
||||
@Entity(name = "historyEntry")
|
||||
public class HistoryEntry {
|
||||
|
||||
@Id
|
||||
@Column(name = "historyEntryId")
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long historyEntryId;
|
||||
|
||||
@Basic
|
||||
private Long date;
|
||||
|
||||
@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
|
||||
// @JoinTable(
|
||||
// name="User_Role",
|
||||
// joinColumns = @JoinColumn( name="User_ID"))
|
||||
private Door door;
|
||||
|
||||
@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
|
||||
private User user;
|
||||
|
||||
public Long getHistoryEntryId() {
|
||||
return historyEntryId;
|
||||
}
|
||||
|
||||
public void setHistoryEntryId(Long historyEntryId) {
|
||||
this.historyEntryId = historyEntryId;
|
||||
}
|
||||
|
||||
public Long getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Long date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Door getDoor() {
|
||||
return door;
|
||||
}
|
||||
|
||||
public void setDoor(Door door) {
|
||||
this.door = door;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package application.network.response;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by Marcin on 2015-01-31.
|
||||
*/
|
||||
public class BaseResponse implements Serializable {
|
||||
private boolean success;
|
||||
private String errorMessage;
|
||||
private Integer errorCode;
|
||||
|
||||
public BaseResponse init(boolean success) {
|
||||
this.success = success;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public String getErrorMessage() {
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
public void setErrorMessage(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
public Integer getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
public void setErrorCode(Integer errorCode) {
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package application.repositories;
|
||||
|
||||
import application.model.HistoryEntry;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
* Created by Marcin on 2015-01-31.
|
||||
*/
|
||||
public interface HistoryEntryRepository extends CrudRepository<HistoryEntry, Long> {
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package application.utils;
|
||||
|
||||
import application.model.*;
|
||||
import application.repositories.DoorRepository;
|
||||
import application.repositories.HistoryEntryRepository;
|
||||
import application.repositories.RoleRepository;
|
||||
import application.repositories.UserRepository;
|
||||
|
||||
@@ -16,16 +17,21 @@ public class Initializer {
|
||||
RoleRepository roleRepository;
|
||||
UserRepository userRepository;
|
||||
DoorRepository doorRepository;
|
||||
HistoryEntryRepository historyEntryRepository;
|
||||
|
||||
public Initializer(RoleRepository roleRepository, UserRepository useRoleRepository) {
|
||||
this.roleRepository = roleRepository;
|
||||
this.userRepository = useRoleRepository;
|
||||
}
|
||||
|
||||
public Initializer(RoleRepository roleRepository, UserRepository userRepository, DoorRepository doorRepository) {
|
||||
public Initializer(RoleRepository roleRepository,
|
||||
UserRepository userRepository,
|
||||
DoorRepository doorRepository,
|
||||
HistoryEntryRepository historyEntryRepository) {
|
||||
this.roleRepository = roleRepository;
|
||||
this.userRepository = userRepository;
|
||||
this.doorRepository = doorRepository;
|
||||
this.historyEntryRepository = historyEntryRepository;
|
||||
}
|
||||
|
||||
public void init(){
|
||||
|
||||
Reference in new issue
Block a user