From 37b56b7375d891b5d2879c43263af0aa2978a41b Mon Sep 17 00:00:00 2001 From: aalexeen Date: Mon, 28 Aug 2017 17:18:49 +0500 Subject: [PATCH 1/3] Add Groups Service --- .../io/khasang/teamnote/config/AppConfig.java | 5 + .../teamnote/controller/GroupsController.java | 50 +++++++ .../io/khasang/teamnote/dao/GroupsDao.java | 6 + .../teamnote/dao/impl/GroupsDaoImpl.java | 11 ++ .../io/khasang/teamnote/entity/Groups.java | 29 ++++ .../teamnote/service/GroupsService.java | 46 +++++++ .../service/impl/GroupsServiceImpl.java | 43 ++++++ .../GroupsControllerIntergrationTest.java | 129 ++++++++++++++++++ 8 files changed, 319 insertions(+) create mode 100644 src/main/java/io/khasang/teamnote/controller/GroupsController.java create mode 100644 src/main/java/io/khasang/teamnote/dao/GroupsDao.java create mode 100644 src/main/java/io/khasang/teamnote/dao/impl/GroupsDaoImpl.java create mode 100644 src/main/java/io/khasang/teamnote/entity/Groups.java create mode 100644 src/main/java/io/khasang/teamnote/service/GroupsService.java create mode 100644 src/main/java/io/khasang/teamnote/service/impl/GroupsServiceImpl.java create mode 100644 src/test/java/io/khasang/teamnote/controller/GroupsControllerIntergrationTest.java diff --git a/src/main/java/io/khasang/teamnote/config/AppConfig.java b/src/main/java/io/khasang/teamnote/config/AppConfig.java index ca29e9a..c4b5fd6 100644 --- a/src/main/java/io/khasang/teamnote/config/AppConfig.java +++ b/src/main/java/io/khasang/teamnote/config/AppConfig.java @@ -80,4 +80,9 @@ public MessageDao messageDao(){ public TaskDao taskDao(){ return new TaskDaoImpl(Task.class); } + + @Bean + public GroupsDao GroupsDao(){ + return new GroupsDaoImpl(Groups.class); + } } diff --git a/src/main/java/io/khasang/teamnote/controller/GroupsController.java b/src/main/java/io/khasang/teamnote/controller/GroupsController.java new file mode 100644 index 0000000..d1e3abf --- /dev/null +++ b/src/main/java/io/khasang/teamnote/controller/GroupsController.java @@ -0,0 +1,50 @@ +package io.khasang.teamnote.controller; + +import io.khasang.teamnote.entity.Groups; +import io.khasang.teamnote.service.GroupsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Controller +@RequestMapping(value = "/groups") +public class GroupsController { + private final GroupsService groupsService; + + @Autowired + public GroupsController(GroupsService groupsService) { + this.groupsService = groupsService; + } + + @RequestMapping(value = "/add", method = RequestMethod.PUT, produces = "application/json;charset=utf-8") + @ResponseBody + public Groups addGroups(@RequestBody Groups groups) { + return groupsService.addGroups(groups); + } + + @RequestMapping(value = "/all", method = RequestMethod.GET) + @ResponseBody + public List getGroupss(){ + return groupsService.getList(); + } + + @RequestMapping(value = "/get/{id}", method = RequestMethod.GET) + @ResponseBody + public Groups getById(@PathVariable(value = "id") String inputId){ + return groupsService.getById(Long.parseLong(inputId)); + } + + @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json;charset=utf-8") + @ResponseBody + public Groups update(@RequestBody Groups groups){ + return groupsService.update(groups); + } + + @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE) + @ResponseBody + public Groups delete(@PathVariable(value = "id") String inputId){ + return groupsService.delete(Long.parseLong(inputId)); + } +} diff --git a/src/main/java/io/khasang/teamnote/dao/GroupsDao.java b/src/main/java/io/khasang/teamnote/dao/GroupsDao.java new file mode 100644 index 0000000..123ab3a --- /dev/null +++ b/src/main/java/io/khasang/teamnote/dao/GroupsDao.java @@ -0,0 +1,6 @@ +package io.khasang.teamnote.dao; + +import io.khasang.teamnote.entity.Groups; + +public interface GroupsDao extends BasicDao{ +} diff --git a/src/main/java/io/khasang/teamnote/dao/impl/GroupsDaoImpl.java b/src/main/java/io/khasang/teamnote/dao/impl/GroupsDaoImpl.java new file mode 100644 index 0000000..e598ef0 --- /dev/null +++ b/src/main/java/io/khasang/teamnote/dao/impl/GroupsDaoImpl.java @@ -0,0 +1,11 @@ +package io.khasang.teamnote.dao.impl; + +import io.khasang.teamnote.dao.GroupsDao; +import io.khasang.teamnote.entity.Groups; + +public class GroupsDaoImpl extends BasicDaoImpl implements GroupsDao { + + public GroupsDaoImpl(Class entityClass) { + super(entityClass); + } +} diff --git a/src/main/java/io/khasang/teamnote/entity/Groups.java b/src/main/java/io/khasang/teamnote/entity/Groups.java new file mode 100644 index 0000000..6d526ec --- /dev/null +++ b/src/main/java/io/khasang/teamnote/entity/Groups.java @@ -0,0 +1,29 @@ +package io.khasang.teamnote.entity; + +import javax.persistence.*; + +@Entity +@Table(name = "groups") +public class Groups { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + private String groupName; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getGroupName() { + return groupName; + } + + public void setGroupName(String groupName) { + this.groupName = groupName; + } +} diff --git a/src/main/java/io/khasang/teamnote/service/GroupsService.java b/src/main/java/io/khasang/teamnote/service/GroupsService.java new file mode 100644 index 0000000..5e5a3db --- /dev/null +++ b/src/main/java/io/khasang/teamnote/service/GroupsService.java @@ -0,0 +1,46 @@ +package io.khasang.teamnote.service; + +import io.khasang.teamnote.entity.Groups; + +import java.util.List; + +public interface GroupsService { + /** + * Add groups to DB + * + * @param groups - groups for creation + * @return created groups + */ + Groups addGroups(Groups groups); + + /** + * Find groups at database + * + * @param id = uniq id at db for specific groups + * @return groups + */ + Groups getById(long id); + + /** + * Delete document from DB + * + * @param id = uniq id groups from DB + * @return deleted groups + */ + Groups delete(long id); + + /** + * Receive all groupss by specific type + * + * @return list from groups + */ + List getList(); + + /** + * Update specify groups + * + * @param groups = groups for update + * @return updated groups + */ + Groups update(Groups groups); +} diff --git a/src/main/java/io/khasang/teamnote/service/impl/GroupsServiceImpl.java b/src/main/java/io/khasang/teamnote/service/impl/GroupsServiceImpl.java new file mode 100644 index 0000000..ac9a5bb --- /dev/null +++ b/src/main/java/io/khasang/teamnote/service/impl/GroupsServiceImpl.java @@ -0,0 +1,43 @@ +package io.khasang.teamnote.service.impl; + +import io.khasang.teamnote.dao.GroupsDao; +import io.khasang.teamnote.entity.Groups; +import io.khasang.teamnote.service.GroupsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service("GroupsService") +public class GroupsServiceImpl implements GroupsService{ + + @Autowired + protected GroupsDao groupsDao; + + + @Override + public Groups addGroups(Groups groups) { + return groupsDao.create(groups); + } + + @Override + public Groups getById(long id) { + return groupsDao.getById(id); + } + + @Override + public Groups delete(long id) { + Groups groupsForDelete = groupsDao.getById(id); + return groupsDao.delete(groupsForDelete); + } + + @Override + public List getList() { + return groupsDao.getList(); + } + + @Override + public Groups update(Groups groups) { + return groupsDao.update(groups); + } +} diff --git a/src/test/java/io/khasang/teamnote/controller/GroupsControllerIntergrationTest.java b/src/test/java/io/khasang/teamnote/controller/GroupsControllerIntergrationTest.java new file mode 100644 index 0000000..1d223b4 --- /dev/null +++ b/src/test/java/io/khasang/teamnote/controller/GroupsControllerIntergrationTest.java @@ -0,0 +1,129 @@ +package io.khasang.teamnote.controller; + +import io.khasang.teamnote.entity.Groups; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.web.client.RestTemplate; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@Ignore +public class GroupsControllerIntergrationTest { + private final String ROOT = "http://localhost:8080/groups"; + private final String ADD = "/add"; + private final String GET = "/get"; + private final String ALL = "/all"; + private final String DELETE = "/delete"; + private final String UPDATE = "/update"; + + @Test + public void addGroupsAndGet() { + Groups groups = createGroups(); + + RestTemplate restTemplate = new RestTemplate(); + + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + GET + "/{id}", + HttpMethod.GET, + null, + Groups.class, + groups.getId() + ); + assertEquals("OK", responseEntity.getStatusCode().getReasonPhrase()); + Groups resultGroups = responseEntity.getBody(); + assertEquals(groups.getGroupName(), resultGroups.getGroupName()); + deleteGroups(resultGroups.getId()); + } + + @Test + public void getAllGroupss() { + RestTemplate restTemplate = new RestTemplate(); + + Groups firstGroups = createGroups(); + Groups secondGroups = createGroups(); + + ResponseEntity> responseEntity = restTemplate.exchange( + ROOT + ALL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>() { + } + ); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + List resultList = responseEntity.getBody(); + assertNotNull(resultList); + deleteGroups(firstGroups.getId()); + deleteGroups(secondGroups.getId()); + } + + @Test + public void updateGroups(){ + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + + Groups firstGroups = createGroups(); + firstGroups.setGroupName("This is a new group name"); + + HttpEntity httpEntity = new HttpEntity<>(firstGroups, headers); + RestTemplate template = new RestTemplate(); + + Groups result = template.exchange( + ROOT + UPDATE, + HttpMethod.POST, + httpEntity, + Groups.class).getBody(); + + assertNotNull(result); + assertNotNull(result.getId()); + assertEquals("This is a new group name", result.getGroupName()); + + deleteGroups(firstGroups.getId()); + } + + private Groups createGroups() { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + + Groups groups = prefillGroups(); + + HttpEntity httpEntity = new HttpEntity<>(groups, headers); + RestTemplate template = new RestTemplate(); + + Groups result = template.exchange( + ROOT + ADD, + HttpMethod.PUT, + httpEntity, + Groups.class).getBody(); + + assertNotNull(result); + assertEquals("FamilyTest", result.getGroupName()); + assertNotNull(result.getId()); + return result; + + } + + private Groups prefillGroups() { + Groups groups = new Groups(); + groups.setGroupName("FamilyTest"); + return groups; + } + + private void deleteGroups(long id) { + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + DELETE + "/{id}", + HttpMethod.DELETE, + null, + String.class, + id + ); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + } +} From c689c45ab44554994c71d058836b6317fef06d02 Mon Sep 17 00:00:00 2001 From: aalexeen Date: Wed, 30 Aug 2017 15:08:43 +0500 Subject: [PATCH 2/3] add view for MessageService --- .../teamnote/controller/AppController.java | 5 + web/WEB-INF/views/js/messageTestService.js | 92 +++++++++++++++++++ web/WEB-INF/views/message.jsp | 79 ++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 web/WEB-INF/views/js/messageTestService.js create mode 100644 web/WEB-INF/views/message.jsp diff --git a/src/main/java/io/khasang/teamnote/controller/AppController.java b/src/main/java/io/khasang/teamnote/controller/AppController.java index b7ac75f..2b501ca 100644 --- a/src/main/java/io/khasang/teamnote/controller/AppController.java +++ b/src/main/java/io/khasang/teamnote/controller/AppController.java @@ -79,4 +79,9 @@ public String RolePage(){ public String getUsersPage(Model model) { return "users"; } + + @RequestMapping("/message") + public String messagePage() { + return "message"; + } } diff --git a/web/WEB-INF/views/js/messageTestService.js b/web/WEB-INF/views/js/messageTestService.js new file mode 100644 index 0000000..f827615 --- /dev/null +++ b/web/WEB-INF/views/js/messageTestService.js @@ -0,0 +1,92 @@ +var service = '/message'; +var RestGet = function (id) { + $.ajax({ + type: 'GET', + url: service + '/get/' + id, + dataType: 'json', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('#response').html(JSON.stringify(jqXHR)) + } + }); +}; + +var RestPut = function (fromUserId, toUserId, messageText) { + var JSONObject = { + 'fromUserId': fromUserId, + 'toUserId': toUserId, + 'messageText': messageText + }; + + $.ajax({ + type: 'PUT', + url: service + "/add", + contentType: 'application/json;charset=utf-8', + data: JSON.stringify(JSONObject), + dataType: 'json', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('$response').html(JSON.stringify(jqXHR)) + } + }); +}; + +var RestGetAll = function () { + $.ajax({ + type: 'GET', + url: service + "/all", + dataType: 'json', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('#response').html(JSON.stringify(jqXHR)) + } + }); +}; + +var RestDelete = function (id) { + $.ajax({ + type: 'DELETE', + url: service + '/delete/' + id, + dataType: 'json', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('#response').html(JSON.stringify(jqXHR)) + } + }); +}; + +var RestPost = function (id, fromUserId, toUserId, messageText) { + var JSONObject = { + 'id': id, + 'fromUserId': fromUserId, + 'toUserId': toUserId, + 'messageText': messageText + }; + + $.ajax({ + type: 'POST', + url: service + '/update', + contentType: 'application/json;charset=utf-8', + data: JSON.stringify(JSONObject), + dataType: 'json', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('#response').html(JSON.stringify(jqXHR)) + } + }) +} \ No newline at end of file diff --git a/web/WEB-INF/views/message.jsp b/web/WEB-INF/views/message.jsp new file mode 100644 index 0000000..b035bc4 --- /dev/null +++ b/web/WEB-INF/views/message.jsp @@ -0,0 +1,79 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Messages + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IDMESSAGE_NAMEMESSAGE_DESCRIPTION
GET MESSAGE BY IDGET/message/get/{id} + Id: + +
Add MESSAGEPUT/message/add +
+ fromUserId: + toUserId: + messageText: + +
+
Update MESSAGEPOST/message/update +
+ id: + fromUserId: + toUserId: + messageText: + +
+
GET ALL MESSAGESGEL ALL/message/all + +
Delete MESSAGE by idDELETE/message/delete/{id} + Id: + +
+ +
+
+ RESPONSE +
+
+
+ + + From beb49c682433579051566ab1a1fd3523e8fd2d9b Mon Sep 17 00:00:00 2001 From: aalexeen Date: Wed, 30 Aug 2017 16:49:50 +0500 Subject: [PATCH 3/3] add view for GroupsService --- .../teamnote/controller/AppController.java | 5 ++ web/WEB-INF/views/groups.jsp | 75 ++++++++++++++++ web/WEB-INF/views/js/groupsTestService.js | 88 +++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 web/WEB-INF/views/groups.jsp create mode 100644 web/WEB-INF/views/js/groupsTestService.js diff --git a/src/main/java/io/khasang/teamnote/controller/AppController.java b/src/main/java/io/khasang/teamnote/controller/AppController.java index 2b501ca..41d780c 100644 --- a/src/main/java/io/khasang/teamnote/controller/AppController.java +++ b/src/main/java/io/khasang/teamnote/controller/AppController.java @@ -84,4 +84,9 @@ public String getUsersPage(Model model) { public String messagePage() { return "message"; } + + @RequestMapping("/groups") + public String groupsPage() { + return "groups"; + } } diff --git a/web/WEB-INF/views/groups.jsp b/web/WEB-INF/views/groups.jsp new file mode 100644 index 0000000..9738a92 --- /dev/null +++ b/web/WEB-INF/views/groups.jsp @@ -0,0 +1,75 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Groups + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IDGROUPS_NAMEGROUPS_DESCRIPTION
GET GROUPS BY IDGET/groups/get/{id} + Id: + +
Add GROUPSPUT/groups/add +
+ groupName: + +
+
Update GROUPSPOST/groups/update +
+ id: + groupName: + +
+
GET ALL GROUPSGEL ALL/groups/all + +
Delete GROUPS by idDELETE/groups/delete/{id} + Id: + +
+ +
+
+ RESPONSE +
+
+
+ + + diff --git a/web/WEB-INF/views/js/groupsTestService.js b/web/WEB-INF/views/js/groupsTestService.js new file mode 100644 index 0000000..700659a --- /dev/null +++ b/web/WEB-INF/views/js/groupsTestService.js @@ -0,0 +1,88 @@ +var service = '/groups'; +var RestGet = function (id) { + $.ajax({ + type: 'GET', + url: service + '/get/' + id, + dataType: 'json', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('#response').html(JSON.stringify(jqXHR)) + } + }); +}; + +var RestPut = function (groupName) { + var JSONObject = { + 'groupName': groupName + }; + + $.ajax({ + type: 'PUT', + url: service + "/add", + contentType: 'application/json;charset=utf-8', + data: JSON.stringify(JSONObject), + dataType: 'json', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('$response').html(JSON.stringify(jqXHR)) + } + }); +}; + +var RestGetAll = function () { + $.ajax({ + type: 'GET', + url: service + "/all", + dataType: 'json', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('#response').html(JSON.stringify(jqXHR)) + } + }); +}; + +var RestDelete = function (id) { + $.ajax({ + type: 'DELETE', + url: service + '/delete/' + id, + dataType: 'json', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('#response').html(JSON.stringify(jqXHR)) + } + }); +}; + +var RestPost = function (id, groupName) { + var JSONObject = { + 'id': id, + 'groupName': groupName + }; + + $.ajax({ + type: 'POST', + url: service + '/update', + contentType: 'application/json;charset=utf-8', + data: JSON.stringify(JSONObject), + dataType: 'json', + async: false, + success: function (result) { + $('#response').html(JSON.stringify(result)) + }, + error: function (jqXHR, testStatus, errorThrown) { + $('#response').html(JSON.stringify(jqXHR)) + } + }) +} \ No newline at end of file