Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.adlternative.tinyhacknews.controller;

import com.adlternative.tinyhacknews.entity.News;
import com.adlternative.tinyhacknews.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class NewsController {

@Autowired
private NewsService newsService;

@PostMapping
public News createNews(@RequestBody News news) {
return newsService.createNews(news);
}

@GetMapping("/{id}")
public News getNewsById(@PathVariable("id") Long id) {
return newsService.getNewsById(id);
}

@GetMapping
public List<News> getAllNews() {
return newsService.getAllNews();
}

@PutMapping("/{id}")
public News updateNews(@PathVariable("id") Long id, @RequestBody News news) {
return newsService.updateNews(id, news);
}

@DeleteMapping("/{id}")
public void deleteNews(@PathVariable("id") Long id) {
newsService.deleteNews(id);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/adlternative/tinyhacknews/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ CREATE TABLE Users (
createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updatedAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE News (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
author VARCHAR(50) NOT NULL,
createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updatedAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
21 changes: 21 additions & 0 deletions src/main/java/com/adlternative/tinyhacknews/entity/News.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.adlternative.tinyhacknews.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class News {
private Long id;
private String title;
private String content;
private String author;
private Date createdAt;
private Date updatedAt;
}
33 changes: 33 additions & 0 deletions src/main/java/com/adlternative/tinyhacknews/mapper/NewsMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.adlternative.tinyhacknews.mapper;

import com.adlternative.tinyhacknews.entity.News;
import java.util.List;
import java.util.Optional;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

@Mapper
@Component
public interface NewsMapper {

@Insert("INSERT INTO News (title, content, author, createdAt, updatedAt) VALUES (#{title}, #{content}, #{author}, #{createdAt}, #{updatedAt})")
@Options(useGeneratedKeys = true, keyProperty = "id")
int insert(News news);

@Select("SELECT * FROM News WHERE id = #{id}")
Optional<News> selectById(Long id);

@Select("SELECT * FROM News")
List<News> selectAll();

@Update("UPDATE News SET title=#{title}, content=#{content}, author=#{author}, updatedAt=#{updatedAt} WHERE id=#{id}")
int update(News news);

@Delete("DELETE FROM News WHERE id=#{id}")
int delete(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.adlternative.tinyhacknews.service;

import com.adlternative.tinyhacknews.entity.News;
import java.util.List;

public interface NewsService {
News createNews(News news);
News getNewsById(Long id);
List<News> getAllNews();
News updateNews(Long id, News news);
void deleteNews(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.adlternative.tinyhacknews.service.impl;

import com.adlternative.tinyhacknews.entity.News;
import com.adlternative.tinyhacknews.mapper.NewsMapper;
import com.adlternative.tinyhacknews.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

@Autowired
private NewsMapper newsMapper;

@Override
public News createNews(News news) {
newsMapper.insert(news);
return news;
}

@Override
public News getNewsById(Long id) {
return newsMapper.selectById(id).orElse(null);
}

@Override
public List<News> getAllNews() {
return newsMapper.selectAll();
}

@Override
public News updateNews(Long id, News news) {
news.setId(id);
newsMapper.update(news);
return news;
}

@Override
public void deleteNews(Long id) {
newsMapper.delete(id);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ spring:
username: tiny-hacknews
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.adlternative.tinyhacknews.entity