Skip to content

Commit

Permalink
Enum and other properties added
Browse files Browse the repository at this point in the history
  • Loading branch information
hamitmizrak committed Jan 11, 2025
1 parent ff9b822 commit 2ce990e
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/interfaces/IRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ import { Ilogin } from "./Ilogin";
// Extends: interface ekledim
export interface IRegister extends Ilogin {
confirmPassword: string;

}
1 change: 1 addition & 0 deletions src/interfaces/Ilogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
export interface Ilogin {
username:string;
password:string;
rememberMe?:boolean; // Optional Property

} // end of interface Ilogin
8 changes: 7 additions & 1 deletion src/models/Admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export class Admin extends User {

// Role Normalde User'dı, Admin olarak değiştirildi
public override getRole(): string {
return "Admin";
//return "Admin";
return UserRole.Admin;
}

// Admin Bütün kullanıcları Listele
public viewAllUser():string {
return "Admin users are listed";
}
} // end class Admin
43 changes: 16 additions & 27 deletions src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,29 @@ export class User {
private id: number;
protected username: string;
private password: string;
private readonly role: string = "User"; // Default Value: User
//private readonly role: string = "User"; // Default Value: User
private readonly role: string = UserRole; // Default Value: User
private lastLogin: Date | null = null; // en son admin giriş tarihi

// constructor: Kurucu Method
constructor(username: string, password: string) {
constructor(username: string, password: string, role:UserRole=UserRole.User) {
this.id = User.idCounter++;
this.username = username;
this.password = password;
this.role = role;
} // end of constructor

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Function: Method
public updateLastLogin(): void {
this.lastLogin = new Date();
}

public getLastLogin(): Date | null {
return this.lastLogin;
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Encapsulation: Getter Method
// ID
public getId(): number {
Expand All @@ -38,29 +52,4 @@ export class User {
public getRole(): string {
return this.role;
}

} // end of class User

/*
// Define the properties of the User class
id: number;
name: string;
email: string;
password: string;
role: string;
created_at: Date;
updated_at: Date;
deleted_at: Date;
// Define the constructor of the User class
constructor(id: number, name: string, email: string, password: string, role: string, created_at: Date, updated_at: Date, deleted_at: Date) {
this.id = id;
this.name = name;
this.email = email;
this.password = password;
this.role = role;
this.created_at = created_at;
this.updated_at = updated_at;
this.deleted_at = deleted_at;
}
*/
11 changes: 10 additions & 1 deletion src/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ export class AuthService {
if (!user.verifyPassword(data.password)) {
return "Password is incorrect";
}

return `Welcome ${user.getRole()} ${user.getUsername}`;
}

// Users (User'ları getir)
public getUsers(): User[] {
return this.users;
}

// User Sil
public deleteUser(username:string):boolean {
const user = this.users.findIndex((u) => u.getUsername() === username);
if (user !== -1) {
this.users.splice(user, 1);
return true;
}
return false;
}
}
49 changes: 37 additions & 12 deletions src/services/UserManagerService.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
import { User } from "../models/User";

// Generics Class
export class UserManager <T extends User> {
// User import
private users: T[] = [];
export class UserManager<T extends User> {
// User import
private users: T[] = [];

// addUser
public addUser(user: T): void {
this.users.push(user);
}
// Kullanıcı Ekle
public addUser(user: T): void {
this.users.push(user);
}

// Bütün Kullanıcıları Getir
public listUsers(): T[] {
return this.users;
}
// Kullanıcıları Listele
public listUsers(): T[] {
return this.users;
}

// Kullanıcıyı Username Göre Bul
public findUserByUsername(username: string): T | undefined {
return this.users.find((u) => u.getUsername() === username);
}

} // end of class UserManager
// Kullanıcıyı Username Göre Bul
public findUserByUserId(userId: number): T | undefined {
return this.users.find((u) => u.getId() === userId);
}

// Kullanıcı Obje'ye Göre Sil
public removeUser(user: T): void {
const index = this.users.indexOf(user);
this.users.splice(index, 1);
}

// Kullanıcı ID'ye Göre Sil
public removeUserById(userId: number): boolean {
const user = this.users.findIndex((u) => u.getId() === userId);
if (user !== -1) {
this.users.splice(user, 1);
return true;
}
return false;
}
} // end of class UserManager
28 changes: 18 additions & 10 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import { User } from "../models/User";

// abstract
export abstract class UserService {
// User import
protected users: User[] = [];

// User import
protected users: User[] = [];
// Gövdesiz Method
public abstract addUser(user: User): void;

// Gövdesiz Method
public abstract addUser(user: User): void;
// Gövdeli Method
public listUsers(): User[] {
return this.users;
}

// Gövdeli Method
public listUsers(): User[] {
return this.users;
}

} // end of class UserService
// Kullanıcı Sayısı
public countUsers(): number {
return this.users.length;
}

// Kullanıcı Sil
public clearUsers(): void {
this.users = [];
}
} // end of class UserService
12 changes: 12 additions & 0 deletions src/utils/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ export class Logger {
message = `[ERROR]: ${message}`;
console.log(message);
}

// WARN
public static warn(message: string): void {
message = `[WARN]: ${message}`;
console.log(message);
}

// DEBUG
public static debug(message: string): void {
message = `[DEBUG]: ${message}`;
console.log(message);
}
} // end of class Logger
5 changes: 5 additions & 0 deletions src/utils/UserRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum UserRole {
User = "User",
Admin = "Admin",
Moderator = "Moderator",
}

0 comments on commit 2ce990e

Please sign in to comment.