Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/io/khasang/teamnote/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ public MessageDao messageDao(){
public TaskDao taskDao(){
return new TaskDaoImpl(Task.class);
}

@Bean
public GroupsDao GroupsDao(){
return new GroupsDaoImpl(Groups.class);
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/khasang/teamnote/controller/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@ public String RolePage(){
public String getUsersPage(Model model) {
return "users";
}

@RequestMapping("/message")
public String messagePage() {
return "message";
}

@RequestMapping("/groups")
public String groupsPage() {
return "groups";
}
}
50 changes: 50 additions & 0 deletions src/main/java/io/khasang/teamnote/controller/GroupsController.java
Original file line number Diff line number Diff line change
@@ -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<Groups> 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));
}
}
6 changes: 6 additions & 0 deletions src/main/java/io/khasang/teamnote/dao/GroupsDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.khasang.teamnote.dao;

import io.khasang.teamnote.entity.Groups;

public interface GroupsDao extends BasicDao<Groups>{
}
11 changes: 11 additions & 0 deletions src/main/java/io/khasang/teamnote/dao/impl/GroupsDaoImpl.java
Original file line number Diff line number Diff line change
@@ -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<Groups> implements GroupsDao {

public GroupsDaoImpl(Class<Groups> entityClass) {
super(entityClass);
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/khasang/teamnote/entity/Groups.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
46 changes: 46 additions & 0 deletions src/main/java/io/khasang/teamnote/service/GroupsService.java
Original file line number Diff line number Diff line change
@@ -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<Groups> getList();

/**
* Update specify groups
*
* @param groups = groups for update
* @return updated groups
*/
Groups update(Groups groups);
}
Original file line number Diff line number Diff line change
@@ -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<Groups> getList() {
return groupsDao.getList();
}

@Override
public Groups update(Groups groups) {
return groupsDao.update(groups);
}
}
Original file line number Diff line number Diff line change
@@ -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<Groups> 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<List<Groups>> responseEntity = restTemplate.exchange(
ROOT + ALL,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<Groups>>() {
}
);

assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
List<Groups> 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<Groups> 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<Groups> 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<String> responseEntity = restTemplate.exchange(
ROOT + DELETE + "/{id}",
HttpMethod.DELETE,
null,
String.class,
id
);

assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
}
}
Loading