Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added scripts for creating tables with logs #797

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
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
9 changes: 6 additions & 3 deletions CharlieBackend.Business/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public async Task<Result<AccountRoleDto>> GrantRoleToAccount(
Account user = await _unitOfWork.AccountRepository
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please find more places where account is modified.

.GetAccountCredentialsByEmailAsync(accountRole.Email);

user.UpdatedByAccountId = _currentUserService.AccountId;

Result<AccountRoleDto> result = null;

if (user == null)
Expand Down Expand Up @@ -123,6 +125,7 @@ public async Task<Result<AccountRoleDto>> RevokeRoleFromAccount(
Account user = await _unitOfWork.AccountRepository
.GetAccountCredentialsByEmailAsync(accountRole.Email);

user.UpdatedByAccountId = _currentUserService.AccountId;
Result<AccountRoleDto> result = null;

if (user == null)
Expand Down Expand Up @@ -254,7 +257,7 @@ public Task<bool> IsEmailChangableToAsync(long id, string newEmail)

public async Task<bool> DisableAccountAsync(long id)
{
var isSucceeded = await _unitOfWork.AccountRepository.DisableAccountAsync(id);
var isSucceeded = await _unitOfWork.AccountRepository.DisableAccountAsync(id, _currentUserService.AccountId);

await _unitOfWork.CommitAsync();

Expand All @@ -263,7 +266,7 @@ public async Task<bool> DisableAccountAsync(long id)

public async Task<bool> EnableAccountAsync(long id)
{
var isSucceeded = await _unitOfWork.AccountRepository.EnableAccountAsync(id);
var isSucceeded = await _unitOfWork.AccountRepository.EnableAccountAsync(id, _currentUserService.AccountId);

await _unitOfWork.CommitAsync();

Expand All @@ -290,7 +293,7 @@ public async Task<Result<AccountDto>> ChangePasswordAsync(ChangeCurrentPasswordD
{
user.Salt = PasswordHelper.GenerateSalt();
user.Password = PasswordHelper.HashPassword(changePassword.NewPassword, user.Salt);

user.UpdatedByAccountId = _currentUserService.AccountId;
await _unitOfWork.CommitAsync();

return Result<AccountDto>.GetSuccess(_mapper.Map<AccountDto>(user));
Expand Down
2 changes: 2 additions & 0 deletions CharlieBackend.Core/Entities/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ public partial class Account : BaseEntity
public virtual ICollection<Student> Students { get; set; }

public virtual ICollection<Secretary> Secretaries { get; set; }

public long UpdatedByAccountId { get; set; }
}
}
2 changes: 2 additions & 0 deletions CharlieBackend.Core/Entities/Course.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public Course()
public virtual ICollection<MentorOfCourse> MentorsOfCourses { get; set; }

public virtual ICollection<StudentGroup> StudentGroup { get; set; }

public long UpdatedByAccountId { get; set; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please set property where needed.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public void Configure(EntityTypeBuilder<Account> entity)
.HasColumnType("VARCHAR(32)")
.HasComment("Standard salt size is 128 bits or 32 characters");

entity.Property(e => e.UpdatedByAccountId)
.IsRequired()
.HasColumnName("UpdatedByAccountId")
.HasDefaultValueSql("1");

entity.Property(e => e.IsActive)
.IsRequired()
.HasColumnName("IsActive")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public void Configure(EntityTypeBuilder<Course> entity)
{
entity.ToTable("Courses");

entity.Property(e => e.UpdatedByAccountId)
.IsRequired()
.HasColumnName("UpdatedByAccountId")
.HasDefaultValueSql("1");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove


entity.Property(e => e.Id)
.IsRequired()
.HasColumnName("ID");
Expand Down
6 changes: 4 additions & 2 deletions CharlieBackend.Data/Repositories/Impl/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public async Task<bool> IsEmailChangableToAsync(long id, string newEmail)
return foundAccount?.IsActive;
}

public async Task<bool> DisableAccountAsync(long id)
public async Task<bool> DisableAccountAsync(long id, long updatedBy)
{
var foundAccount = await _applicationContext.Accounts
.FirstOrDefaultAsync(account => account.Id == id);
Expand All @@ -110,10 +110,11 @@ public async Task<bool> DisableAccountAsync(long id)
return false;
}
foundAccount.IsActive = false;
foundAccount.UpdatedByAccountId = updatedBy;
return true;
}

public async Task<bool> EnableAccountAsync(long id)
public async Task<bool> EnableAccountAsync(long id, long updatedBy)
{
var foundAccount = await _applicationContext.Accounts
.FirstOrDefaultAsync(account => account.Id == id);
Expand All @@ -123,6 +124,7 @@ public async Task<bool> EnableAccountAsync(long id)
}

foundAccount.IsActive = true;
foundAccount.UpdatedByAccountId=updatedBy;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public interface IAccountRepository : IRepository<Account>

public Task<bool?> IsAccountActiveAsync(string email);

Task<bool> DisableAccountAsync(long id);
Task<bool> DisableAccountAsync(long id, long editedBy);

Task<bool> EnableAccountAsync(long id);
Task<bool> EnableAccountAsync(long id, long editedBy);

public void UpdateAccountCredentials(Account account);

Expand Down
2 changes: 1 addition & 1 deletion CharlieBackend.Panel/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void AddServices(this IServiceCollection services, IConfiguration

services.AddHttpClient<IHttpUtil, HttpUtil>(client =>
{
client.BaseAddress = new Uri(configuration.GetSection("Urls:Api:Https").Value);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert

client.BaseAddress = new Uri(configuration.GetSection("Urls:Api:Http").Value);
})
.SetHandlerLifetime(Timeout.InfiniteTimeSpan);
}
Expand Down
9 changes: 9 additions & 0 deletions scripts/addLogsTabs/10_add_UpdatedByAccountId_forAccount.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
USE `Soft`;

LOCK TABLES `Accounts` WRITE;

ALTER TABLE `Accounts`
ADD `UpdatedByAccountId` BIGINT UNSIGNED NOT NULL DEFAULT 1;

UNLOCK TABLES;

64 changes: 64 additions & 0 deletions scripts/addLogsTabs/11_add_logs_AccountChanges.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
USE `Soft`;

DROP TABLE IF EXISTS `AccountsChanges`;

CREATE TABLE `AccountsChanges` (
`ID` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`AccountID` BIGINT UNSIGNED NOT NULL,
`OldRole` TINYINT UNSIGNED DEFAULT NULL,
`NewRole` TINYINT UNSIGNED DEFAULT NULL,
`OldFirstName` VARCHAR(30) DEFAULT NULL,
`NewFirstName` VARCHAR(30) DEFAULT NULL,
`OldLastName` VARCHAR(30) DEFAULT NULL,
`NewLastName` VARCHAR(30) DEFAULT NULL,
`OldEmail` VARCHAR(50) DEFAULT NULL,
`NewEmail` VARCHAR(50) DEFAULT NULL,
`OldPasswordHash` VARCHAR(64) DEFAULT NULL,
`NewPasswordHash` VARCHAR(64) DEFAULT NULL,
`OldSalt` VARCHAR(32) DEFAULT NULL,
`NewSalt` VARCHAR(32) DEFAULT NULL,
`OldIsActive` BIT DEFAULT NULL,
`NewIsActive` BIT DEFAULT NULL,
`OldForgotPasswordToken` VARCHAR(36) DEFAULT NULL,
`NewForgotPasswordToken` VARCHAR(36) DEFAULT NULL,
`OldForgotTokenGenDate` DATETIME DEFAULT NULL,
`NewForgotTokenGenDate` DATETIME DEFAULT NULL,
`OldAvatarID` BIGINT UNSIGNED DEFAULT NULL,
`NewAvatarID` BIGINT UNSIGNED DEFAULT NULL,
`QueriedBy` BIGINT UNSIGNED NOT NULL,
`DateTime` DATETIME DEFAULT NOW(),

CONSTRAINT `PK_AccountsChanges` PRIMARY KEY (`ID`)
);

DELIMITER $$

CREATE TRIGGER `AfterAccountsInsert`
AFTER INSERT
ON `Accounts` FOR EACH ROW
BEGIN
INSERT INTO `AccountsChanges`
(`AccountID`, `NewRole`, `NewFirstName`, `NewLastName`, `NewEmail`, `NewPasswordHash`, `NewSalt`, `NewIsActive`, `NewForgotPasswordToken`, `NewForgotTokenGenDate`, `NewAvatarID`, `QueriedBy`)
VALUES
(NEW.`ID`, NEW.`Role`, NEW.`FirstName`, NEW.`LastName`, NEW.`Email`, NEW.`PasswordHash`, NEW.`Salt`, NEW.`IsActive`, NEW.`ForgotPasswordToken`, NEW.`ForgotTokenGenDate`, NEW.`AvatarID`, NEW.`UpdatedByAccountId`)
;
END$$

DELIMITER ;

DELIMITER $$

CREATE TRIGGER `AfterAccountsUpdate`
AFTER UPDATE
ON `Accounts` FOR EACH ROW
BEGIN
INSERT INTO `AccountsChanges`
(`AccountID`, `OldRole`, `NewRole`, `OldFirstName`, `NewFirstName`, `OldLastName`, `NewLastName`, `OldEmail`, `NewEmail`, `OldPasswordHash`, `NewPasswordHash`, `OldSalt`, `NewSalt`, `OldIsActive`, `NewIsActive`, `OldForgotPasswordToken`, `NewForgotPasswordToken`,
`OldForgotTokenGenDate`, `NewForgotTokenGenDate`, `OldAvatarID`, `NewAvatarID`, `QueriedBy`)
VALUES
(NEW.`ID`, OLD.`Role`, NEW.`Role`, OLD.`FirstName`, NEW.`FirstName`, OLD.`LastName`, NEW.`LastName`, OLD.`Email`, NEW.`Email`, OLD.`PasswordHash`, NEW.`PasswordHash`, OLD.`Salt`, NEW.`Salt`, OLD.`IsActive`, NEW.`IsActive`, OLD.`ForgotPasswordToken`, NEW.`ForgotPasswordToken`,
OLD.`ForgotTokenGenDate`, NEW.`ForgotTokenGenDate`, OLD.`AvatarID`, NEW.`AvatarID`, NEW.`UpdatedByAccountId`)
;
END$$

DELIMITER ;
9 changes: 9 additions & 0 deletions scripts/addLogsTabs/12_add_UpdatedByAccountId_forCourses.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
USE Soft;

LOCK TABLES Courses WRITE;

ALTER TABLE Courses

ADD UpdatedByAccountId BIGINT UNSIGNED NOT NULL DEFAULT 1;

UNLOCK TABLES;
46 changes: 46 additions & 0 deletions scripts/addLogsTabs/13_add_logs_CoursesChanges.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
USE `Soft`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do dev testing


DROP TABLE IF EXISTS `CoursesChanges`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rename courses -> course


CREATE TABLE `CoursesChanges` (
`ID` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`CourseID` BIGINT UNSIGNED NOT NULL,
`OldName` TINYINT UNSIGNED DEFAULT NULL,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please set appropriate types

`NewName` TINYINT UNSIGNED DEFAULT NULL,
`OldIsActive` BIT DEFAULT NULL,
`NewIsActive` BIT DEFAULT NULL,
`QueriedBy` BIGINT UNSIGNED NOT NULL,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rename to updated by account id

`DateTime` DATETIME DEFAULT NOW(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update date


CONSTRAINT `PK_CoursesChanges` PRIMARY KEY (`ID`)
);

DELIMITER $$

CREATE TRIGGER `AfterCoursesInsert`
AFTER INSERT
ON `Courses` FOR EACH ROW
BEGIN
INSERT INTO `CoursesChanges`
(`CourseID`, `NewName`, `NewIsActive`, `QueriedBy`)
VALUES
(NEW.`ID`, NEW.`Name`, NEW.`IsActive`, NEW.`UpdatedByAccountId`)
;
END$$

DELIMITER ;

DELIMITER $$

CREATE TRIGGER `AfterCoursesUpdate`
AFTER UPDATE
ON `Courses` FOR EACH ROW
BEGIN
INSERT INTO `CoursesChanges`
(`CourseID`, `OldName`, `NewName`,`OldIsActive`, `NewIsActive`, `QueriedBy`)
VALUES
(NEW.`ID`, OLD.`Name`, NEW.`Name`, OLD.`IsActive`, NEW.`IsActive`, NEW.`UpdatedByAccountId`)
;
END$$

DELIMITER ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
USE Soft;

LOCK TABLES Homeworks WRITE;

ALTER TABLE Homeworks

ADD UpdatedByAccountId BIGINT UNSIGNED NOT NULL DEFAULT 1;

UNLOCK TABLES;
46 changes: 46 additions & 0 deletions scripts/addLogsTabs/15_add_logs_HomeworksChanges.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
USE `Soft`;

DROP TABLE IF EXISTS `HomeworksChanges`;

CREATE TABLE `HomeworksChanges` (
`ID` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`HomeworkID` BIGINT UNSIGNED NOT NULL,
`OldDueDate` DATETIME NOT NULL COMMENT 'Use UTC time',
`NewDueDate` DATETIME NOT NULL COMMENT 'Use UTC time',
`OldTaskText` VARCHAR(8000) NOT NULL,
`NewTaskText` VARCHAR(8000) NOT NULL,
`QueriedBy` BIGINT UNSIGNED NOT NULL,
`DateTime` DATETIME DEFAULT NOW(),

CONSTRAINT `PK_HomeworksChanges` PRIMARY KEY (`ID`)
);

DELIMITER $$

CREATE TRIGGER `AfterHomeworksInsert`
AFTER INSERT
ON `Homeworks` FOR EACH ROW
BEGIN
INSERT INTO `HomeworksChanges`
(`HomeworkID`, `NewDueDate`, `NewTaskText`, `QueriedBy`)
VALUES
(NEW.`ID`, NEW.`DueDate`, NEW.`TaskText`, NEW.`UpdatedByAccountId`)
;
END$$

DELIMITER ;

DELIMITER $$

CREATE TRIGGER `AfterHomeworksUpdate`
AFTER UPDATE
ON `Homeworks` FOR EACH ROW
BEGIN
INSERT INTO `HomeworksChanges`
(`HomeworkID`, `OldDueDate`, `NewDueDate`,`OldTaskText`, `NewTaskText`, `QueriedBy`)
VALUES
(NEW.`ID`, OLD.`DueDate`, NEW.`DueDate`, OLD.`TaskText`, NEW.`TaskText`, NEW.`UpdatedByAccountId`)
;
END$$

DELIMITER ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
USE Soft;

LOCK TABLES Studentgroups WRITE;

ALTER TABLE Studentgroups

ADD UpdatedByAccountId BIGINT UNSIGNED NOT NULL DEFAULT 1;

UNLOCK TABLES;
49 changes: 49 additions & 0 deletions scripts/addLogsTabs/17_add_logs_StudentGroupsChanges.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
USE `Soft`;

DROP TABLE IF EXISTS `StudentGroupsChanges`;

CREATE TABLE `StudentGroupsChanges` (
`ID` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`StudentGroupID` BIGINT UNSIGNED NOT NULL,
`OldName` VARCHAR(100) NOT NULL,
`NewName` VARCHAR(100) NOT NULL,
`OldStartDate` DATE NOT NULL,
`NewStartDate` DATE NOT NULL,
`OldFinishDate` DATE NOT NULL,
`NewFinishDate` DATE NOT NULL,
`OldIsActive` BIT DEFAULT NULL,
`NewIsActive` BIT DEFAULT NULL,
`QueriedBy` BIGINT UNSIGNED NOT NULL,
`DateTime` DATETIME DEFAULT NOW(),
CONSTRAINT `PK_StudentGroupsChanges` PRIMARY KEY (`ID`)
);

DELIMITER $$

CREATE TRIGGER `AfterStudentGroupsInsert`
AFTER INSERT
ON `studentgroups` FOR EACH ROW
BEGIN
INSERT INTO `StudentGroupsChanges`
(`StudentGroupID`,`NewName`,`NewStartDate`,`NewFinishDate`, `NewIsActive`, `QueriedBy`)
VALUES
(NEW.`ID`, NEW.`Name`, NEW.`StartDate`, NEW.`FinishDate`, NEW.`IsActive`, NEW.`UpdatedByAccountId`)
;
END$$

DELIMITER ;

DELIMITER $$

CREATE TRIGGER `AfterStudentGroupsUpdate`
AFTER UPDATE
ON `studentgroups` FOR EACH ROW
BEGIN
INSERT INTO `StudentGroupsChanges`
(`StudentGroupID`, `OldName`, `NewName`, `OldStartDate`, `NewStartDate`,`OldFinishDate`,`NewFinishDate`,`OldIsActive`, `NewIsActive`, `QueriedBy`)
VALUES
(NEW.`ID`, OLD.`Name`, NEW.`Name`, OLD.`StartDate`, NEW.`StartDate`,OLD.`FinishDate`,NEW.`FinishDate`, OLD.`IsActive`, NEW.`IsActive`, NEW.`UpdatedByAccountId`)
;
END$$

DELIMITER ;
Loading