Commit 2464b142 authored by nmuthusamy's avatar nmuthusamy

Initial commit

parent 88708410
package com.altimetrik.articles;
package com.altimetrik;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......
package com.altimetrik.articles.controllers;
import java.util.Set;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.CrossOrigin;
import com.altimetrik.articles.entity.Article;
import com.altimetrik.articles.util.ArticlesUtil;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
@CrossOrigin
@Controller
@RequestMapping("/articles")
@Slf4j
public class ArticleController {
@Autowired
ArticlesUtil articleUtil;
@GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.ALL_VALUE)
public ResponseEntity<List<Article>> getALL() {
log.info("Inside get all method");
return new ResponseEntity(articleUtil.call(), HttpStatus.OK);
}
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.ALL_VALUE)
public ResponseEntity<List<Article>> getById(@PathVariable("id") Integer id) {
log.info("Inside getbyId method");
return new ResponseEntity(articleUtil.call(id), HttpStatus.OK);
}
@PostMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.ALL_VALUE)
public ResponseEntity<Article> createArticle(@RequestBody Article article) {
log.info("Inside creat method");
return new ResponseEntity(articleUtil.create(article), HttpStatus.OK);
}
@PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.ALL_VALUE)
public ResponseEntity<Article> updateArticle(@PathVariable("id") Integer id) {
log.info("Inside update method");
try {
return new ResponseEntity(articleUtil.update(id), HttpStatus.OK);
}
catch (Exception e) {
log.error("Error while update : {}", e.getMessage());
}
return new ResponseEntity(null, HttpStatus.EXPECTATION_FAILED);
}
}
package com.altimetrik.articles.entity;
import java.util.ArrayList;
import java.util.List;
public class Article implements Comparable<Article> {
private Integer id;
private String title;
private String description;
private List<String> tags = new ArrayList<>();
private Long vote;
private String createdDate;
private String createdBy;
private String updatedDate;
private String updatedBy;
public Article() {
super();
}
public Article(Integer id, String title, String description, List<String> tags, Long vote, String createdDate,
String createdBy, String updatedDate, String updatedBy) {
super();
this.id = id;
this.title = title;
this.description = description;
this.tags = tags;
this.vote = vote;
this.createdDate = createdDate;
this.createdBy = createdBy;
this.updatedDate = updatedDate;
this.updatedBy = updatedBy;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
public Long getVote() {
return vote;
}
public void setVote(Long vote) {
this.vote = vote;
}
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(String updatedDate) {
this.updatedDate = updatedDate;
}
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}
@Override
public int compareTo(Article o) {
if (o.getVote() > this.getVote()) {
return 2;
}
else if (o.getVote() < this.getVote()) {
return -1;
}
else {
return 0;
}
}
}
package com.altimetrik.articles.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.altimetrik.articles.controllers.ArticleController;
import com.altimetrik.articles.entity.Article;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Slf4j
@Component
public class ArticlesUtil {
private static final Logger log = LoggerFactory.getLogger(ArticleController.class);
@Value("${article.fileName}")
private String configFileName;
public static List<Article> fullList;
public int count=4;
@PostConstruct
public void init() {
try {
ClassLoader classLoader = new ArticlesUtil().getClass().getClassLoader();
InputStream io = classLoader.getResourceAsStream(configFileName);
fullList = new ObjectMapper().readValue(io, new TypeReference<List<Article>>() {
});
fullList.forEach(a -> System.out.println(a.toString()));
log.info("Validator json loaded successfully");
}
catch (JsonParseException e) {
log.info("Error while parsing the validator json : " + e);
}
catch (JsonMappingException e) {
log.info("Error while mapping the validator json : " + e);
}
catch (IOException e) {
log.info("Error while reading the validator json : " + e);
}
}
public List<Article> call() {
Collections.sort(fullList);
return fullList;
}
public Article call(int id) {
for (Article article : fullList) {
if (article.getId() == id) {
return article;
}
}
return null;
}
public Article create(Article article) {
article.setId(count);
fullList.add(article);
count++;
// this.addToFile();
return article;
}
/*
* private void addToFile() { try { URL url = new
* ArticlesUtil().getClass().getClassLoader().getResource(configFileName);
* FileWriter f = new FileWriter(url.toURI().getPath()); Gson gson = new
* Gson();
*
* f.write(gson.toJson(fullList)); f.close(); } catch (URISyntaxException e)
* { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
*
* }
*/
public Article update(int id) throws Exception {
Article obj = this.call(id);
if (fullList.remove(obj)) {
Long l = obj.getVote();
obj.setVote(++l);
fullList.add(obj);
}
else {
throw new Exception("No data found to update");
}
// addToFile();
return obj;
}
}
......@@ -6,13 +6,12 @@
* and shall use it only in accordance with the terms and conditions
* entered into with Altimetrik.
*/
package com.altimetrik.articles.config;
package com.altimetrik.config;
import static springfox.documentation.builders.PathSelectors.regex;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -44,7 +43,7 @@ public class SwaggerConfig {
private ApiInfo metaData() {
return new ApiInfo("Playground Authentication and Authorization", "Playground API Services for Articles", "2.0",
return new ApiInfo("Playground Application", "Playground API Services", "2.0",
"https://www.altimetrik.com/privacy-policy/", new Contact("Playground", "https://playground.altimetrik.com", "pg-mgr1@altimetrik.com"),
"Apache License Version 2.0", "https://www.apache.org/licenses/LICENSE-2.0", Collections.emptyList());
}
......
package com.altimetrik.controllers;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/hello")
public class Hello {
@GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.ALL_VALUE)
public ResponseEntity<String> getUserDetailByGitlabEmailId() {
return new ResponseEntity<>("Wecome to Playground...", HttpStatus.OK);
}
}
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