21.02.24 Untact프로젝트(게시물 삭제, 수정, 현재날짜 적용 등)

2021. 2. 24. 20:51JAVA/Spring & Vue APP 프로젝트(백엔드)

<UsrArticleController.java>

@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", "해당 게시물은 존재하지 않습니다.");
		}

		rs.put("id", id);

		return rs;
	}

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

		String updateDate = Util.getNowDateStr();
		
		Article selArticle = null;

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

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

		if (selArticle == null) {
			rs.put("resultCode", "F-1");
			rs.put("msg", "해당 게시물은 존재하지 않습니다.");
			rs.put("id", id);
			return rs;
		}

		selArticle.setUpdateDate(updateDate);
		selArticle.setTitle(title);
		selArticle.setBody(body);

		rs.put("resultCode", "S-1");
		rs.put("msg", "성공");

		rs.put("id", id);

		return rs;
	}

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

		return false;
	}

<Util.java>

// 현재 날짜를 String으로 리턴하는 유틸
	public static String getNowDateStr() {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date time = new Date();
		return format.format(time);
	}