Commit f518a3ca authored by Esma Meral's avatar Esma Meral
Browse files

initial commit

parents
No related merge requests found
Showing with 394 additions and 0 deletions
+394 -0
.gitignore 0 → 100644
.DS_Store
.mvn/
mvnw
mvnw.cmd
HELP.md
/target/
!.mvn/wrapper/maven-wrapper.jar
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.vscode/
readme.md 0 → 100644
# Service project for data requirements
**todo** project contains rest services for creating, updating, deleting and saving todos
## Running project from Eclipse
- Run eclipse
- Select "Import/Existing Maven Project" from File menu
- Select "services" project
- Right click on **Application.java** and choose "Run As/Java Application"
- After a while, Spring project will start
- You can make request through the url: http://localhost:8080/todos
## Running project from Command Line
Alternatively, you can run the jar file provided from the command line:
java -jar todos.jar
## Provided Rest services
### Getting all todos
- **URL:** [http://localhost:8080/todos](http://localhost:8080/todos)
- **Method:** GET
- **Path Variable:** NA
- **Request Body:** NA
- **Response Body:** Todo list
### Getting a specific todo
- **URL:** [http://localhost:8080/todos/{id}](http://localhost:8080/todos/{id})
- **Method:** GET
- **Path Variable:** id of todo to be read
- **Request Body:** NA
- **Response Body:** Todo
### Deleting a todo
- **URL:** [http://localhost:8080/todos/{id}](http://localhost:8080/todos/{id})
- **Method:** DELETE
- **Path Variable:** id of todo to be deleted
- **Request Body:** NA
- **Response Body:** deleted todo
### Updating a todo
- **URL:** [http://localhost:8080/todos/{id}](http://localhost:8080/todos/{id})
- **Method:** PUT
- **Path Variable:** id of todo to be updated
- **Request Body:** todo data
- **Response Body:** updated todo
### Saving a todo
- **URL:** [http://localhost:8080/todo](http://localhost:8080/todo)
- **Method:** POST
- **Path Variable:** NA
- **Request Body:** todo data
- **Response Body:** saved todo. The id value will be assigned randomly.
## Exceptional Cases
To test exceptional cases, simply send a request as follows:
[http://localhost:8080/todo?code=111](http://localhost:8080/todo?code=111)
In this case, the server will return an error code of 500 when request contains a parameter named "code"
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
### VS Code ###
.vscode/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.todo</groupId>
<artifactId>todo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>todo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.todo;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TodoApplication.class);
}
}
package com.todo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TodoApplication {
public static void main(String[] args) {
SpringApplication.run(TodoApplication.class, args);
}
}
package com.todo.model;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Todo {
private int id;
private String text;
private Date createDate;
public Todo() {
super();
this.createDate=new Date();
}
public Todo(int id, String text) {
super();
this.id = id;
this.text=text;
this.createDate=new Date();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
package com.todo.services;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.todo.model.Todo;
@RestController
@RequestMapping("/todos")
@CrossOrigin
public class StoreService {
@Autowired
TodoServiceStub service;
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<List<Todo>> getAll(HttpServletRequest request) {
String code = request.getParameter("code");
if (code != null) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ArrayList<Todo>());
} else {
return ResponseEntity.status(HttpStatus.OK).body(service.getTodos());
}
}
@RequestMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public Todo getById(@PathVariable("id") int id) {
return service.get(id);
}
@RequestMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.DELETE)
public Todo removeById(@PathVariable("id") int id) {
return service.remove(id);
}
@RequestMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.PUT)
public Todo update(@PathVariable("id") int id, @RequestBody Todo todo) {
return service.update(id, todo);
}
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
public Todo create(@RequestBody Todo todo) {
return service.save(todo);
}
}
package com.todo.services;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import com.todo.model.Todo;
@Component
public class TodoServiceStub {
private List<Todo> todos = new ArrayList<Todo>();
private TodoServiceStub() {
super();
init();
}
public Todo update(int id, Todo aTodo) {
Todo old = get(id);
todos.remove(old);
aTodo.setId(id);
todos.add(aTodo);
return aTodo;
}
public Todo remove(int id) {
Todo todo = get(id);
todos.remove(todo);
return todo;
}
public Todo save(Todo todo) {
todo.setId((int)(Math.random()*100+10));
todos.add(todo);
return todo;
}
public Todo get(int id) {
Todo todo = null;
for (Todo t : todos) {
if (t.getId()==id)
todo = t;
}
return todo;
}
private void init() {
Todo t1 = new Todo(1, "Lorem ipsum dolor sit amet, eam justo petentium et, mel cu ubique dolorum oportere, his ei quando utamur. ");
Todo t2 = new Todo(2, "Diam ridens copiosae eu sea. Eu vix harum nonumes incorrupte. ");
Todo t3 = new Todo(3, "Pericula ocurreret eos ea, vidit verterem ne vel, ei commodo vidisse electram cum. ");
Todo t4 = new Todo(4, "Ei stet exerci epicurei vim, aperiam explicari te vix, ad ludus oportere eam. Te eleifend delicata tincidunt his, sed error movet no. Ius ea oblique sensibus, ad dicam verterem usu.");
Todo t5 = new Todo(5, "No qui modus molestie vivendum. Altera maiorum efficiendi ad per. ");
todos.add(t1);
todos.add(t2);
todos.add(t3);
todos.add(t4);
todos.add(t5);
}
public List<Todo> getTodos() {
return todos;
}
}
package com.todo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class TodoApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment