Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Kol #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Kol #34

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
90 changes: 90 additions & 0 deletions chaincode/src/contracts/fabric-student-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

'use strict';

const { Contract } = require('fabric-contract-api');
const { ClientIdentity } = require('fabric-shim');

class FabricStudentStorage extends Contract {
constructor() {
super('org.fabric.keyvaluestorage');
}

async createStudentRec(data, studentEmail, fullName) {
const id = new ClientIdentity(data.stub);
if (id.cert.subject.orgUnitName !== 'admin') {
throw new Error("Access error");
}
const recordAsBytes = await data.stub.getState(studentEmail);

if (recordAsBytes && recordAsBytes.toString().length !== 0) {
throw new Error("User already exists");
}

const record = {
fullName: fullName,
semesters: []
}
const newRecordInBytes = Buffer.from(JSON.stringify(record));
await data.stub.putState(studentEmail, newRecordInBytes);
return JSON.stringify(record, null, 4);
}

async getRecords(data, email) {
const identity = new ClientIdentity(data.stub);
if (identity.cert.subject.orgUnitName !== 'admin') {
throw new Error("Access error");
}
const record = await data.stub.getState(email);
if (!record || record.toString().length === 0) {
throw new Error("No user with this email");
}
return JSON.parse(record.toString());
}

async updateRecords(data, email, record) {
const recordBytes = Buffer.from(JSON.stringify(record));
await data.stub.putState(email, recordBytes);
}

async addSubject(data, studentEmail, semester, subject) {
const record = await getStudentRecord(data, studentEmail);
const teacherEmail = identity.cert.subject.commonName;
if (!record.semesters[semester]) {
record.semesters[semester] = {};
}
record.semesters[semester][subject] = {
lector: teacherEmail,
themes: []
}
await updateStudentRecord(data, studentEmail, record);
return JSON.stringify(record, null, 4);
}

async createGrade(data, studentEmail, semester, subject, theme, grade) {
const record = await getStudentRecord(data, studentEmail);
if (!record.semesters[semester]?.[subject]) {
throw new Error("This subject is absent in the specified semester");
}
record.semesters[semester][subject].themes.push([
{
title: theme,
rating: grade,
date: Date.now()
}
]);
await updateStudentRecord(data, studentEmail, record);
return JSON.stringify(record, null, 4);
}

async getAllGrades(data, studentEmail) {
const record = await getStudentRecord(data, studentEmail);
return JSON.stringify(record.semesters, null, 4);
}

async getStudentGradesBySemester(data, studentEmail, semester) {
const record = await getStudentRecord(data, studentEmail);
return JSON.stringify(record.semesters[semester] || [], null, 4);
}
}

module.exports = FabricStudentStorage;