Skip to content

Commit 7df4e02

Browse files
committed
Veritabanı Güncelleme ve Raporlama
Veritabanı Güncelleme: Yeni bir tablo olan "Öğrenci_Sayaç" veritabanına eklenir. Tablo yalnızca tek bir satır ve sütun içerir (sayaç). Sayaç değeri sıfırdan başlar ve öğrenci eklendiğinde veya silindiğinde güncellenir. Haftalık Raporlama: .env dosyasında saklanacak bir "periyot" değişkeni belirlenir. Bu değişken, haftalık yedekleme zamanlarını hesaplamak için kullanılır ve tetikleme ile e-posta gönderimini sağlar. Her hafta, mevcut tüm öğrencilerin bir listesi veritabanından okunur ve JSON formatında bir dosyaya yazılır. Bu yedek dosyası, belirli bir e-posta adresine e-posta eki olarak gönderilir.
0 parents  commit 7df4e02

39 files changed

+3316
-0
lines changed

.gitignore

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Serverless directories
108+
.serverless/
109+
110+
# FuseBox cache
111+
.fusebox/
112+
113+
# DynamoDB Local files
114+
.dynamodb/
115+
116+
# TernJS port file
117+
.tern-port
118+
119+
# Stores VSCode versions used for testing VSCode extensions
120+
.vscode-test
121+
122+
# yarn v2
123+
.yarn/cache
124+
.yarn/unplugged
125+
.yarn/build-state.yml
126+
.yarn/install-state.gz
127+
.pnp.*

Schema.sql

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CREATE TABLE IF NOT EXISTS Ogrenci (
2+
id SERIAL PRIMARY KEY,
3+
name VARCHAR(100),
4+
email VARCHAR(100),
5+
deptid INTEGER,
6+
counter INTEGER DEFAULT 0
7+
);
8+
9+
CREATE TABLE IF NOT EXISTS Bolum (
10+
id SERIAL PRIMARY KEY,
11+
name VARCHAR(100),
12+
dept_std_id INTEGER
13+
);
14+
CREATE TABLE IF NOT EXISTS Ogrenci_sayac (
15+
sayac INTEGER DEFAULT 0
16+
);
17+
INSERT INTO Ogrenci_sayac (sayac) VALUES (0);

controller.js

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
const fs = require('fs');
2+
const pool = require("./db");
3+
4+
const createTables = async () => {
5+
try {
6+
const sql = fs.readFileSync('schema.sql', 'utf8');
7+
await pool.query(sql);
8+
console.log('Tablolar oluşturuldu.');
9+
} catch (error) {
10+
console.error('Hata:', error);
11+
}
12+
};
13+
14+
// STUDENT CONTROLLER
15+
// Öğrenci verilerini alma
16+
const fetchAllStudents = async () => {
17+
try {
18+
const result = await pool.query('SELECT * FROM Ogrenci');
19+
return result.rows;
20+
} catch (err) {
21+
console.error('Hata:', err);
22+
throw err;
23+
}
24+
};
25+
26+
const getAllStudent = async (req, res) => {
27+
try {
28+
const result = await pool.query('SELECT * FROM Ogrenci');
29+
return res.status(200).send({ status: true, message: 'Kayıt listelendi.', data: result.rows });
30+
} catch (err) {
31+
console.error('Hata:', err);
32+
return res.status(500).send({ status: false, message: 'Kayıt Listeleme Sırasında Hata Oluştu. Hata: ' + err });
33+
} finally {
34+
}
35+
};
36+
37+
const getStudentById = async (req, res) => {
38+
const id = req.params.id;
39+
try {
40+
const result = await pool.query('SELECT * FROM Ogrenci WHERE id = $1', [id]);
41+
return res.status(200).send({ status: true, message: 'Öğrenci bulundu.', data: result.rows });
42+
} catch (err) {
43+
console.error('Hata:', err);
44+
return res.status(500).send({ status: false, message: 'Öğrenci Bulma Sırasında Hata Oluştu. Hata: ' + err });
45+
} finally {
46+
}
47+
};
48+
// const updaterCouter=async()=>{
49+
// let counter=await fetchAllStudents();
50+
// if(!counter){
51+
// return false;
52+
// }else{
53+
// return counter.length;
54+
// }
55+
// }
56+
const createStudent = async (req, res) => {
57+
const { name, email, deptid } = req.body;
58+
try {
59+
const result = await pool.query('INSERT INTO Ogrenci (name, email, deptid) VALUES ($1, $2, $3) RETURNING *', [name, email, deptid]);
60+
61+
// Öğrenci oluşturulduktan sonra sayaç güncellendi
62+
await pool.query('UPDATE Ogrenci_sayac SET sayac = sayac + 1');
63+
64+
return res.status(201).send({ status: true, message: 'Öğrenci oluşturuldu.', data: result.rows });
65+
} catch (err) {
66+
console.error('Hata:', err);
67+
return res.status(500).send({ status: false, message: 'Öğrenci Oluşturma Sırasında Hata Oluştu. Hata: ' + err });
68+
} finally {
69+
}
70+
};
71+
72+
73+
const updateStudent = async (req, res) => {
74+
const id = req.params.id;
75+
const { name, email, deptid } = req.body;
76+
77+
const updatedFields = [];
78+
if (name) updatedFields.push('name');
79+
if (email) updatedFields.push('email');
80+
if (deptid) updatedFields.push('deptid');
81+
82+
if (updatedFields.length === 0) {
83+
return res.status(400).send({ status: false, message: 'En az bir alan güncellenmelidir.' });
84+
}
85+
86+
const sql = `UPDATE Ogrenci SET ${updatedFields.map((field) => `${field} = $${updatedFields.indexOf(field) + 1}`).join(', ')} WHERE id = $${updatedFields.length + 1} RETURNING *`;
87+
88+
try {
89+
const result = await pool.query(sql, [...updatedFields.map((field) => req.body[field]), id]);
90+
return res.status(200).send({ status: true, message: 'Öğrenci güncellendi.', data: result.rows });
91+
} catch (err) {
92+
console.error('Hata:', err);
93+
return res.status(500).send({ status: false, message: 'Öğrenci güncelleme sırasında bir hata oluştu.', error: err });
94+
}
95+
};
96+
97+
const deleteStudent = async (req, res) => {
98+
const id = req.params.id;
99+
try {
100+
await pool.query('DELETE FROM Ogrenci WHERE id = $1', [id]);
101+
102+
// Öğrenci silindikten sonra sayaç güncellendi
103+
await pool.query('UPDATE Ogrenci_sayac SET sayac = sayac - 1');
104+
105+
return res.status(200).send({ status: true, message: 'Öğrenci silindi.' });
106+
} catch (err) {
107+
console.error('Hata:', err);
108+
return res.status(500).send({ status: false, message: 'Öğrenci Silme Sırasında Hata Oluştu. Hata: ' + err });
109+
} finally {
110+
}
111+
};
112+
113+
const getStudentCount = async (req, res) => {
114+
try {
115+
const result = await pool.query('SELECT sayac FROM Ogrenci_sayac');
116+
const count = result.rows[0].sayac;
117+
return res.status(200).send({ status: true, message: 'Öğrenci sayısı alındı.', data: count });
118+
} catch (err) {
119+
console.error('Hata:', err);
120+
return res.status(500).send({ status: false, message: 'Öğrenci Sayısı Alınırken Hata Oluştu. Hata: ' + err });
121+
}
122+
};
123+
124+
125+
// BOLUM CONTROLLER
126+
127+
const getAllBolum = async (req, res) => {
128+
try {
129+
const result = await pool.query('SELECT * FROM Bolum');
130+
return res.status(200).send({ status: true, message: 'Kayıt listelendi.', data: result.rows });
131+
} catch (err) {
132+
console.error('Hata:', err);
133+
return res.status(500).send({ status: false, message: 'Kayıt Listeleme Sırasında Hata Oluştu. Hata: ' + err });
134+
} finally {
135+
}
136+
};
137+
138+
const getBolumById = async (req, res) => {
139+
const id = req.params.id;
140+
try {
141+
const result = await pool.query('SELECT * FROM Bolum WHERE id = $1', [id]);
142+
return res.status(200).send({ status: true, message: 'Bölüm bulundu.', data: result.rows });
143+
} catch (err) {
144+
console.error('Hata:', err);
145+
return res.status(500).send({ status: false, message: 'Bölüm Bulma Sırasında Hata Oluştu. Hata: ' + err });
146+
} finally {
147+
}
148+
};
149+
150+
const createBolum = async (req, res) => {
151+
const { name, dept_std_id } = req.body;
152+
try {
153+
154+
const result = await pool.query('INSERT INTO Bolum (name, dept_std_id) VALUES ($1, $2) RETURNING *', [name, dept_std_id]);
155+
156+
return res.status(201).send({ status: true, message: 'Bölüm oluşturuldu.', data: result.rows });
157+
} catch (err) {
158+
console.error('Hata:', err);
159+
return res.status(500).send({ status: false, message: 'Bölüm oluşturma sırasında bir hata oluştu.', error: err });
160+
} finally {
161+
}
162+
};
163+
164+
const updateBolum = async (req, res) => {
165+
const id = req.params.id;
166+
const { name, dept_std_id } = req.body;
167+
168+
const updatedFields = [];
169+
if (name) updatedFields.push('name');
170+
if (dept_std_id) updatedFields.push('dept_std_id');
171+
172+
if (updatedFields.length === 0) {
173+
return res.status(400).send({ status: false, message: 'En az bir alan güncellenmelidir.' });
174+
}
175+
176+
const sql = `UPDATE Bolum SET ${updatedFields.map((field) => `${field} = $${updatedFields.indexOf(field) + 1}`).join(', ')} WHERE id = $${updatedFields.length + 1} RETURNING *`;
177+
178+
const values = [...updatedFields.map((field) => req.body[field]), id];
179+
180+
try {
181+
const result = await pool.query(sql, values);
182+
return res.status(200).send({ status: true, message: 'Bölüm güncellendi.', data: result.rows });
183+
} catch (err) {
184+
console.error('Hata:', err);
185+
return res.status(500).send({ status: false, message: 'Bölüm Güncelleme Sırasında Hata Oluştu. Hata: ' + err });
186+
}
187+
};
188+
189+
const deleteBolum = async (req, res) => {
190+
const id = req.params.id;
191+
try {
192+
await pool.query('DELETE FROM Bolum WHERE id = $1', [id]);
193+
return res.status(200).send({ status: true, message: 'Bölüm silindi.' });
194+
} catch (err) {
195+
console.error('Hata:', err);
196+
return res.status(500).send({ status: false, message: 'Bölüm Silme Sırasında Hata Oluştu. Hata: ' + err });
197+
} finally {
198+
}
199+
};
200+
201+
module.exports = {
202+
createTables,
203+
getAllStudent,
204+
getStudentById,
205+
createStudent,
206+
updateStudent,
207+
deleteStudent,
208+
getAllBolum,
209+
getBolumById,
210+
createBolum,
211+
updateBolum,
212+
deleteBolum,
213+
getStudentCount,
214+
fetchAllStudents
215+
216+
};

db.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { Pool } = require('pg');
2+
require('dotenv').config();
3+
4+
const pool = new Pool({
5+
user: process.env.DB_USER,
6+
host: process.env.DB_HOST,
7+
database: process.env.DB_NAME,
8+
password: process.env.DB_PASSWORD,
9+
});
10+
11+
module.exports = pool;

0 commit comments

Comments
 (0)