Skip to content

Commit

Permalink
mapOf 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
wjxor committed Jul 18, 2021
1 parent 75b265e commit 3e5f9eb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
58 changes: 18 additions & 40 deletions src/main/java/com/wjxor/untact/controller/UsrArticleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,87 +43,65 @@ public List<Article> showList() {

@RequestMapping("/usr/article/doAdd")
@ResponseBody
public Map<String, Object> doAdd(String title, String body) {
public Map<String, Object> doAdd(String title, String body) {
String regDate = Util.getNowDateStr();
String updateDate = regDate;

articles.add(new Article(++articlesLastId, regDate, updateDate, title, body));
String updateDate = regDate;

Map<String, Object> rs = new HashMap<>();
rs.put("resultCode", "S-1");
rs.put("msg", "성공하였습니다.");
rs.put("id", articlesLastId);
articles.add(new Article(++articlesLastId, regDate, updateDate, title, body));

return rs;
return Util.mapOf("resultCode", "S-1", "msg", "성공하였습니다.", "id", articlesLastId);
}



@RequestMapping("/usr/article/doDelete")
@ResponseBody
public Map<String, Object> doDelete(int id) {
boolean deleteArticleRs = deleteArticle(id);

Map<String, Object> rs = new HashMap<>();

if (deleteArticleRs) {
rs.put("resultCode", "S-1");
rs.put("msg", "성공하였습니다.");
}

else {
rs.put("resultCode", "F-1");
rs.put("msg", "해당 게시물은 존재하지 않습니다.");

if (deleteArticleRs == false) {
return Util.mapOf("resultCode", "F-1", "msg", "해당 게시물은 존재하지 않습니다.");
}

rs.put("id", id);

return rs;
return Util.mapOf("resultCode", "S-1", "msg", "성공하였습니다.", "id", id);

}

private boolean deleteArticle(int id) {
for (Article article : articles) {
if (article.getId() == id) {
articles.remove(article);

return true;
}
}

return false;

}

@RequestMapping("/usr/article/doModify")
@ResponseBody
public Map<String, Object> doModify(int id, String title, String body) {
Article selArticle = null;

for (Article article : articles) {
if (article.getId() == id) {
selArticle = article;
break;
}
}

Map<String, Object> rs = new HashMap<>();

if (selArticle == null) {
rs.put("resultCode", "F-1");
rs.put("msg", String.format("%d번 게시물은 존재하지 않습니다.", id));

return rs;
return Util.mapOf("resultCode", "F-1", "msg", String.format("%d번 게시물은 존재하지 않습니다.", id));
}

selArticle.setUpdateDate(Util.getNowDateStr());
selArticle.setTitle(title);
selArticle.setBody(body);

rs.put("resultCode", "S-1");
rs.put("msg", String.format("%d번 게시물이 수정되었습니다.", id));
rs.put("id", id);

return rs;
return Util.mapOf("resultCode", "S-1", "msg", String.format("%d번 게시물이 수정되었습니다.", id), "id", id);
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/wjxor/untact/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

public class Util {
public static String getNowDateStr() {
Expand All @@ -10,4 +12,36 @@ public static String getNowDateStr() {

return NowDate.format(time);
}

public static Map<String, Object> mapOf(Object... args) {
if (args.length % 2 != 0) {
throw new IllegalArgumentException("인자를 짝수개 입력해주세요.");
}

int size = args.length / 2;

Map<String, Object> map = new LinkedHashMap<>();

for (int i = 0; i < size; i++) {
int keyIndex = i * 2;
int valueIndex = keyIndex + 1;

String key;
Object value;

try {
key = (String)args[keyIndex];
}

catch (ClassCastException e) {
throw new IllegalArgumentException("키는 String으로 입력해야 합니다. " + e.getMessage());
}

value = args[valueIndex];

map.put(key, value);
}

return map;
}
}

0 comments on commit 3e5f9eb

Please sign in to comment.