Skip to content

Commit

Permalink
Merge branch 'develop' into feat/sato/658-password-reset-page
Browse files Browse the repository at this point in the history
  • Loading branch information
KazumaSun committed Mar 5, 2024
2 parents 674194a + 03d81ca commit 8240fd0
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 52 deletions.
18 changes: 9 additions & 9 deletions api/externals/repository/expense_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ func NewExpenseRepository(c db.Client, ac abstract.Crud) ExpenseRepository {
}

func (er *expenseRepository) All(c context.Context) (*sql.Rows, error) {
query := "SELECT * FROM expense"
query := "SELECT * FROM expenses"
return er.crud.Read(c, query)
}

func (er *expenseRepository) Find(c context.Context, id string) (*sql.Row, error) {
query := "SELECT * FROM expense WHERE id = " + id
query := "SELECT * FROM expenses WHERE id = " + id
return er.crud.ReadByID(c, query)
}

func (er *expenseRepository) Create(c context.Context, name string, yearID string) error {
query := `
INSERT INTO
expense (expense_name,yearID)
expenses (expense_name,yearID)
VALUES
('` + name + "'," + yearID + ")"
return er.crud.UpdateDB(c, query)
Expand All @@ -52,20 +52,20 @@ func (er *expenseRepository) Create(c context.Context, name string, yearID strin
func (er *expenseRepository) Update(c context.Context, id string, name string, yearID string) error {
query := `
UPDATE
expense
expenses
SET expense_name = '` + name +
"', yearID = " + yearID +
" WHERE id = " + id
return er.crud.UpdateDB(c, query)
}

func (er *expenseRepository) Destroy(c context.Context, id string) error {
query := "DELETE FROM expense WHERE id = " + id
query := "DELETE FROM expenses WHERE id = " + id
return er.crud.UpdateDB(c, query)
}

func (er *expenseRepository) FindLatestRecord(c context.Context) (*sql.Row, error) {
query := `SELECT * FROM expense ORDER BY id DESC LIMIT 1`
query := `SELECT * FROM expenses ORDER BY id DESC LIMIT 1`
return er.crud.ReadByID(c, query)
}

Expand Down Expand Up @@ -114,13 +114,13 @@ func (er *expenseRepository) AllByPeriod(c context.Context, year string) (*sql.R
SELECT
*
FROM
expense
expenses
INNER JOIN
years
ON
expense.yearID = years.id
expenses.yearID = years.id
WHERE
years.year = ` + year +
" ORDER BY expense.id;"
" ORDER BY expenses.id;"
return er.crud.Read(c, query)
}
36 changes: 18 additions & 18 deletions mysql/db/expense.sql → mysql/db/expenses.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use finansu_db;

CREATE TABLE expense (
CREATE TABLE expenses (
id int(10) unsigned not null auto_increment,
expense_name varchar(255) not null,
totalPrice int(10) default 0,
Expand All @@ -10,16 +10,16 @@ CREATE TABLE expense (
PRIMARY KEY (id)
);

INSERT into expense (expense_name,yearID) values ("企画局",2);
INSERT into expense (expense_name,yearID) values ("総務局",2);
INSERT into expense (expense_name,yearID) values ("情報局",2);
INSERT into expense (expense_name,yearID) values ("制作局",2);
INSERT into expense (expense_name,yearID) values ("渉外局",2);
INSERT into expense (expense_name,yearID) values ("財務局",2);
INSERT into expense (expense_name,yearID) values ("本部運営費",2);
INSERT into expense (expense_name,yearID) values ("備品整備費",2);
INSERT into expense (expense_name,yearID) values ("備品整備準備費",2);
INSERT into expense (expense_name,yearID) values ("翌年度繰越金",2);
INSERT into expenses (expense_name,yearID) values ("企画局",2);
INSERT into expenses (expense_name,yearID) values ("総務局",2);
INSERT into expenses (expense_name,yearID) values ("情報局",2);
INSERT into expenses (expense_name,yearID) values ("制作局",2);
INSERT into expenses (expense_name,yearID) values ("渉外局",2);
INSERT into expenses (expense_name,yearID) values ("財務局",2);
INSERT into expenses (expense_name,yearID) values ("本部運営費",2);
INSERT into expenses (expense_name,yearID) values ("備品整備費",2);
INSERT into expenses (expense_name,yearID) values ("備品整備準備費",2);
INSERT into expenses (expense_name,yearID) values ("翌年度繰越金",2);


-- 終端文字の変更
Expand Down Expand Up @@ -102,23 +102,23 @@ GROUP BY

-- tmp2のデータをexpeneseに入れる
UPDATE
expense
expenses
INNER JOIN
tmp2
ON
expense.id = tmp2.id
expenses.id = tmp2.id
SET
expense.totalPrice = tmp2.totalPrice;
expenses.totalPrice = tmp2.totalPrice;

-- tmp2のidがNULLのexpenseのtotalPriceを0にする
-- tmp2のidがNULLのexpensesのtotalPriceを0にする
UPDATE
expense
expenses
LEFT JOIN
tmp2
ON
expense.id = tmp2.id
expenses.id = tmp2.id
SET
expense.totalPrice = 0
expenses.totalPrice = 0
WHERE
tmp2.id IS NULL;

Expand Down
108 changes: 108 additions & 0 deletions mysql/feature/add_procedure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
-- 終端文字の変更
DELIMITER //
-- ストアドプロシージャ作成
CREATE PROCEDURE updateExpense()
BEGIN

-- 1 テンポラリテーブル作成tmp,tmp
CREATE TEMPORARY TABLE tmp (
id int(10) NOT NULL,
totalPrice int(10),
purchase_reports_id int(10),
addition int(10),
discount int(10),
expense_id int(10),
finance_check boolean,
PRIMARY KEY (`id`)
);

CREATE TEMPORARY TABLE tmp2 (
id int(10) NOT NULL,
totalPrice int(10) NOT NULL,
PRIMARY KEY (`id`)
);

-- 2 mpにpurchase_itemsのfinansu_checkがtrueのものをpurchase_orderごとに和を入れる
INSERT INTO
tmp(id,totalPrice)
SELECT
pi.purchase_order_id,
SUM(pi.price * pi.quantity)
FROM
purchase_items pi
WHERE
pi.finance_check IS true
GROUP BY
pi.purchase_order_id;

-- 3 tmpにpurhchase_reportsのデータを入れる
UPDATE
tmp
INNER JOIN
purchase_reports pr
ON
tmp.id = pr.purchase_order_id
SET
tmp.purchase_reports_id = pr.id,
tmp.addition = pr.addition,
tmp.discount = pr.discount,
tmp.finance_check = pr.finance_check;

-- tmpにpurchaser_ordersのexpense_idを入れる
UPDATE
tmp
INNER JOIN
purchase_orders po
ON
tmp.id = po.id
SET
tmp.expense_id = po.expense_id
WHERE
po.finance_check IS true;

-- expense_idがNULLのレコードを削除する
DELETE FROM tmp WHERE expense_id IS NULL;

-- tmpのデータをexpense_idごとにまとめて総和を求める、データをtmp2に入れる
INSERT INTO
tmp2(id, totalPrice)
SELECT
tmp.expense_id,
SUM(tmp.totalPrice + tmp.addition - tmp.discount)
FROM
tmp
WHERE
tmp.finance_check IS true
GROUP BY
tmp.expense_id;

-- tmp2のデータをexpeneseに入れる
UPDATE
expenses
INNER JOIN
tmp2
ON
expenses.id = tmp2.id
SET
expenses.totalPrice = tmp2.totalPrice;

-- tmp2のidがNULLのexpensesのtotalPriceを0にする
UPDATE
expenses
LEFT JOIN
tmp2
ON
expenses.id = tmp2.id
SET
expenses.totalPrice = 0
WHERE
tmp2.id IS NULL;

-- テンポラリテーブル削除
DROP TEMPORARY TABLE tmp,tmp2;

END;
//

-- 終端文字戻す
DELIMITER ;
69 changes: 44 additions & 25 deletions view/next-project/src/pages/sponsors/index.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
import clsx from 'clsx';
import type { NextPage } from 'next';
import Head from 'next/head';
import { useState as UseState, useMemo as UseMemo } from 'react';

import { useState, useEffect } from 'react';
import OpenDeleteModalButton from '@/components/sponsors/OpenDeleteModalButton';
import OpenEditModalButton from '@/components/sponsors/OpenEditModalButton';
import { get } from '@/utils/api/api_methods';
import { Card, Title } from '@components/common';
import MainLayout from '@components/layout/MainLayout';
import OpenAddModalButton from '@components/sponsors/OpenAddModalButton';
import { Sponsor } from '@type/common';
import { Sponsor, YearPeriod } from '@type/common';

interface Props {
sponsor: Sponsor[];
yearPeriods: YearPeriod[];
}

const date = new Date();

export const getServerSideProps = async () => {
const getSponsorUrl = process.env.SSR_API_URI + '/sponsors';
const sponsorRes = await get(getSponsorUrl);
const getPeriodsUrl = process.env.SSR_API_URI + '/years/periods';
const periodsRes = await get(getPeriodsUrl);
const getSponsorViewUrl =
process.env.SSR_API_URI +
'/sponsors/' +
(periodsRes ? String(periodsRes[periodsRes.length - 1].year) : String(date.getFullYear()));

return {
props: {
sponsor: sponsorRes,
sponsorView: getSponsorViewUrl,
yearPeriods: periodsRes,
},
};
};

const sponsorship: NextPage<Props> = (props: Props) => {
const sponsorList: Sponsor[] = props.sponsor;
const Sponsorship: NextPage<Props> = (props: Props) => {
const [sponsors, setSponsors] = useState<Sponsor[]>(props.sponsor);

const currentYear = new Date().getFullYear().toString();
const [selectedYear, setSelectedYear] = UseState<string>(currentYear);
const yearPeriods = props.yearPeriods;
const [selectedYear, setSelectedYear] = useState<string>(
yearPeriods ? String(yearPeriods[yearPeriods.length - 1].year) : String(date.getFullYear()),
);

//年度別のsponsorsを取得
const getSponsors = async () => {
const getSponsorViewUrlByYear = process.env.CSR_API_URI + '/sponsors/' + selectedYear;
const getSponsorsByYears = await get(getSponsorViewUrlByYear);
setSponsors(getSponsorsByYears);
};

const filteredSponsorListViews = UseMemo(() => {
return sponsorList.filter((sponsor) => {
return sponsor.createdAt?.includes(selectedYear);
});
}, [sponsorList, selectedYear]);
useEffect(() => {
getSponsors();
}, [selectedYear]);

return (
<MainLayout>
Expand All @@ -50,12 +64,17 @@ const sponsorship: NextPage<Props> = (props: Props) => {
<Title title={'協賛企業一覧'} />
<select
className='w-100'
defaultValue={currentYear}
defaultValue={selectedYear}
onChange={(e) => setSelectedYear(e.target.value)}
>
<option value='2021'>2021</option>
<option value='2022'>2022</option>
<option value='2023'>2023</option>
{props.yearPeriods &&
props.yearPeriods.map((year) => {
return (
<option value={year.year} key={year.id}>
{year.year}年度
</option>
);
})}
</select>
</div>
<div className='hidden justify-end md:flex'>
Expand Down Expand Up @@ -87,10 +106,10 @@ const sponsorship: NextPage<Props> = (props: Props) => {
</tr>
</thead>
<tbody className='border border-x-white-0 border-b-primary-1 border-t-white-0'>
{filteredSponsorListViews &&
filteredSponsorListViews.map((sponsor, index) => (
{sponsors && sponsors.length > 0 ? (
sponsors.map((sponsor, index) => (
<tr
className={clsx(index !== sponsorList.length - 1 && 'border-b')}
className={clsx(index !== sponsors.length - 1 && 'border-b')}
key={sponsor.id}
>
<td className='py-3'>
Expand All @@ -115,8 +134,8 @@ const sponsorship: NextPage<Props> = (props: Props) => {
</div>
</td>
</tr>
))}
{!filteredSponsorListViews.length && (
))
) : (
<tr>
<td colSpan={6} className='py-3'>
<div className='text-center text-black-300'>データがありません</div>
Expand All @@ -134,4 +153,4 @@ const sponsorship: NextPage<Props> = (props: Props) => {
);
};

export default sponsorship;
export default Sponsorship;

0 comments on commit 8240fd0

Please sign in to comment.