Skip to content

Commit

Permalink
Fixed PUT requests and added GET/POST for answers
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim committed Feb 27, 2021
1 parent 161e08c commit ed50e3b
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 151 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
Expand Down Expand Up @@ -62,6 +68,11 @@
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
23 changes: 0 additions & 23 deletions src/main/java/com/sber/demo/BaseResponse.java

This file was deleted.

95 changes: 63 additions & 32 deletions src/main/java/com/sber/demo/Controller.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
package com.sber.demo;

import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.v3.core.util.Json;
import com.google.gson.Gson;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.minidev.json.JSONObject;
import okhttp3.*;
import org.apache.commons.lang.StringEscapeUtils;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.servlet.FilterRegistration;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

import static java.nio.charset.StandardCharsets.*;
import java.io.IOException;
import java.util.Objects;


@Tag(name="Основные запросы", description="Здесь представлены основные методы для взаимодействия")
Expand All @@ -35,6 +24,7 @@ public class Controller {

private static final String PORT_USERS = "8081";
private static final String PORT_QUESTIONS = "8082";
private static final String PORT_ARTICLES = "8083";

private static final String GET = "GET";
private static final String POST = "POST";
Expand All @@ -54,7 +44,10 @@ private String makeRequest(String url, String json, String requestMethod) {

if (json != null) {
okhttp3.RequestBody formBody = okhttp3.RequestBody.create(json, MediaType.parse("application/json"));
builder.post(formBody);
if (requestMethod.equals(POST))
builder.post(formBody);
else if (requestMethod.equals(PUT))
builder.put(formBody);
}

Request request = builder.build();
Expand All @@ -76,41 +69,62 @@ private String makeRequest(String url, String json, String requestMethod) {
description = "Возвращает JSON строку с полями объекта класса User с уникальным ID (Example: user.15)")
@RequestMapping(value = "/api/users/{id}", produces = "application/json", method = RequestMethod.GET)
@ResponseBody()
public String getUser(@PathVariable("id") int id) {
return makeRequest(IP + PORT_USERS + "/users/user." + id, null, GET);
public User getUser(@PathVariable("id") int id) {
Gson gson = new Gson();
return gson.fromJson(makeRequest(IP + PORT_USERS + "/users/user." + id, null, GET), User.class);
}

@Operation(
summary = "Создать объект пользователя",
description = "Принимает JSON с полями объекта класса User")
@PostMapping(value = "/api/users", consumes = "application/json")
public String createUser(@RequestBody String json) {
return makeRequest(IP + PORT_USERS + "/users", json, POST);
public String createUser(@RequestBody User user) {
Gson gson = new Gson();
return makeRequest(IP + PORT_USERS + "/users", gson.toJson(user), POST);
}

@Operation(
summary = "Обновить объект пользователя",
description = "Принимает JSON с полями объекта класса User")
@PutMapping(value = "/api/users", consumes = "application/json")
public String updateUser(@RequestBody String json) {
return makeRequest(IP + PORT_USERS + "/users", json, PUT);
public String updateUser(@RequestBody User user) {
Gson gson = new Gson();
return makeRequest(IP + PORT_USERS + "/users", gson.toJson(user), PUT);
}

@Operation(
summary = "Получить объект статьи",
description = "Возврашает JSON с полями объекта класса Article")
@RequestMapping(value = "/api/articles/{id}", consumes = "application/json", method = RequestMethod.GET)
public String getArticle(@PathVariable("id") int id) {
return makeRequest(IP + PORT_ARTICLES + "/articles/" + id, null, GET);
}

@Operation(
summary = "Создать объект статьи",
description = "Принимает JSON с полями объекта класса Article")
@PostMapping(value = "/api/articles", consumes = "application/json")
public String createArticle(@RequestBody String json) {
return makeRequest(IP + PORT_ARTICLES + "/articles", json, POST);
}

@Operation(
summary = "Создать объект вопроса",
description = "Получает JSON с полями объекта класса Question")
@PostMapping(value = "/api/questions", produces = "application/json")
public String createQuestion(@RequestBody String json) {
return makeRequest(IP + PORT_QUESTIONS + "/questions", json, POST);
public String createQuestion(@RequestBody Question question) {
Gson gson = new Gson();
return makeRequest(IP + PORT_QUESTIONS + "/questions", gson.toJson(question), POST);
}

@Operation(
summary = "Получить объект вопроса",
description = "Возвращает JSON с полями объекта класса Question")
@RequestMapping(value = "/api/questions/{id}", produces = "application/json", method = RequestMethod.GET)
@ResponseBody()
public String getQuestion(@PathVariable("id") int id) {
return makeRequest(IP + PORT_QUESTIONS + "/questions/" + id, null, GET);
public Question getQuestion(@PathVariable("id") int id) {
Gson gson = new Gson();
return gson.fromJson(makeRequest(IP + PORT_QUESTIONS + "/questions/" + id, null, GET), Question.class);
}

@Operation(
Expand All @@ -124,8 +138,25 @@ public String getQuestions(@RequestBody String json) {
@Operation(
summary = "Обновить объект вопроса",
description = "Получает JSON с полями объекта класса Question")
@PutMapping(value = "/api/questions", produces = "application/json")
public String updateQuestion(@RequestBody String json) {
return makeRequest(IP + PORT_QUESTIONS + "/questions", json, PUT);
@PutMapping(value = "/api/questions/{id}", produces = "application/json")
public String updateQuestion(@PathVariable("id") int id, @RequestBody String json) {
return makeRequest(IP + PORT_QUESTIONS + "/questions/" + id, json, PUT);
}

@Operation(
summary = "Получить объект ответа",
description = "Получает JSON с полями объекта класса Answer")
@RequestMapping(value = "/api/answers/{id}", produces = "application/json", method = RequestMethod.GET)
@ResponseBody()
public String getAnswer(@PathVariable("id") int id, @RequestBody String json) {
return makeRequest(IP + PORT_QUESTIONS + "/answers/" + id, json, GET);
}

@Operation(
summary = "Получить объект ответа",
description = "Получает JSON с полями объекта класса Answer")
@PutMapping(value = "/api/answers", produces = "application/json")
public String createAnswer(@RequestBody String json) {
return makeRequest(IP + PORT_QUESTIONS + "/answers/", json, GET);
}
}
18 changes: 0 additions & 18 deletions src/main/java/com/sber/demo/LongQuestion.java

This file was deleted.

115 changes: 115 additions & 0 deletions src/main/java/com/sber/demo/Question.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.sber.demo;

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Date;

@Schema(description = "Короткий вопрос")
public class Question {
private int id;
private int rating;
private int priority;
private String header;
private String tags;
private String content;
private String createdBy;
private String updatedAt;
private String createdAt;
private boolean isSolved;

public Question() {
}

public Question(int id, int rating, int priority, String header, String tags, String content, String createdBy, String updatedAt, String createdAt, boolean isSolved) {
this.id = id;
this.rating = rating;
this.priority = priority;
this.header = header;
this.tags = tags;
this.content = content;
this.createdBy = createdBy;
this.updatedAt = updatedAt;
this.createdAt = createdAt;
this.isSolved = isSolved;
}

public int getPriority() {
return priority;
}

public void setPriority(int priority) {
this.priority = priority;
}

public String getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}

public String getCreatedAt() {
return createdAt;
}

public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getRating() {
return rating;
}

public void setRating(int rating) {
this.rating = rating;
}

public String getHeader() {
return header;
}

public void setHeader(String header) {
this.header = header;
}

public String getTags() {
return tags;
}

public void setTags(String tags) {
this.tags = tags;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public boolean isSolved() {
return isSolved;
}

public void setSolved(boolean solved) {
isSolved = solved;
}

public String getCreatedBy() {
return createdBy;
}

public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
}
Loading

0 comments on commit ed50e3b

Please sign in to comment.