Permalink
Browse files

Merge pull request #1 from SunHigh105/develop

Develop
  • Loading branch information...
SunHigh105 committed Dec 18, 2018
2 parents 535341a + 595abf5 commit b17100805ce78e3fe8c3e4d9f493901359520587
@@ -1,4 +1,39 @@
package income_outgo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Category {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String name;
private String type;

public Long getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}
@@ -1,4 +1,83 @@
package income_outgo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
private IncomeOutgoService incomeOutgoService;

// 基準となる日
private Date today(){
Date today = new Date();
return today;
}

// 今月(yyyy-MM)の文字列
private String thisMonthPath(){

SimpleDateFormat thisMonthPathFormat = new SimpleDateFormat("yyyy-MM");
return thisMonthPathFormat.format(today());
}

//今年(yyyyの文字列)
private String thisYearPath(){
SimpleDateFormat thisYearPathFormat = new SimpleDateFormat("yyyy");
return thisYearPathFormat.format(today());
}

// カテゴリのリスト
private List<Category> categories(String type){
return categoryService.findByType(type);
}

@GetMapping("setting")
public String newcCategory(Model model){
model.addAttribute("categories_outgo", categories("outgo"));
model.addAttribute("categories_income", categories("income"));
//今月をリンクに入れる
model.addAttribute("thisMonthPath", thisMonthPath());
//今年をリンクに入れる
model.addAttribute("thisYearPath", thisYearPath());
return "category/setting";
}
@GetMapping("/{id}/edit")
public String edit(@PathVariable Long id, Model model){
Category category = categoryService.findById(id);
model.addAttribute("category", category);
//今月をリンクに入れる
model.addAttribute("thisMonthPath", thisMonthPath());
//今年をリンクに入れる
model.addAttribute("thisYearPath", thisYearPath());
return "category/edit";
}

@PostMapping("/setting")
public String create(@ModelAttribute Category category){
categoryService.save(category);
return "redirect:/category/setting";
}

@PutMapping("{id}")
public String update(@PathVariable Long id, @ModelAttribute Category category){
category.setId(id);
categoryService.save(category);
return "redirect:/category/setting";
}

@DeleteMapping("{id}")
public String destroy(@PathVariable Long id){
categoryService.deleteById(id);
return "redirect:/category/setting";
}
}

@@ -1,4 +1,12 @@
package income_outgo;

public class CategoryRepository {
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface CategoryRepository extends JpaRepository<Category, Long> {

List<Category> findByType(String type);
// Category findOne(Integer id);
// void delete(Integer id);
}
@@ -0,0 +1,32 @@
package income_outgo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
@Autowired
private CategoryRepository categoryRepository;

public List<Category> findAll(){
return categoryRepository.findAll();
}

public List<Category> findByType(String type){
return categoryRepository.findByType(type);
}

public Category findById(Long id){
return categoryRepository.findById(id).orElse(null);
}

public Category save(Category category){
return categoryRepository.save(category);
}

public void deleteById(Long id){
categoryRepository.deleteById(id);
}
}
@@ -4,39 +4,65 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.sql.Date;

@Entity // This tells Hibernate to make a table out of this class
public class IncomeOutgo {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private Long id;
private Long cost;
private Date date;
private String memo;
private String type;
private Long category_id;

private String name;

private String email;

public Integer getId() {
public Long getId() {
return id;
}

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

public String getName() {
return name;
public Long getCost() {
return cost;
}

public void setCost(Long cost) {
this.cost = cost;
}

public java.util.Date getDate() {
return date;
}

public void setDate(String date) {
this.date = Date.valueOf(date);
}

public String getMemo() {
return memo;
}

public void setMemo(String memo) {
this.memo = memo;
}

public String getType() {
return type;
}

public void setName(String name) {
this.name = name;
public void setType(String type) {
this.type = type;
}

public String getEmail() {
return email;
public Long getCategory_id() {
return category_id;
}

public void setEmail(String email) {
this.email = email;
public void setCategory_id(Long category_id) {
this.category_id = category_id;
}


Oops, something went wrong.

0 comments on commit b171008

Please sign in to comment.