Skip to content

Commit 0ef56d3

Browse files
committed
refactor: replace Role model with UserRole enum
- Removed Role model - Added UserRole enum - Updated User model
1 parent bb32a36 commit 0ef56d3

File tree

10 files changed

+39
-105
lines changed

10 files changed

+39
-105
lines changed

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ analyzer:
33
errors:
44
lines_longer_than_80_chars: ignore
55
no_runtimetype_tostring: ignore
6+
unnecessary_null_checks: ignore

lib/ht_shared.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ library;
44
export 'src/exceptions/exceptions.dart';
55
export 'src/models/models.dart';
66
export 'src/models/permission.dart';
7-
export 'src/models/role.dart';

lib/src/models/role.dart

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/src/models/role.g.dart

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/src/models/user.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:equatable/equatable.dart';
2-
import 'package:ht_shared/src/models/role.dart';
2+
import 'package:ht_shared/src/models/user_role.dart';
33
import 'package:json_annotation/json_annotation.dart';
44

55
part 'user.g.dart';
@@ -29,7 +29,7 @@ class User extends Equatable {
2929
final String? email;
3030

3131
/// The role of the user.
32-
final Role role;
32+
final UserRole role;
3333

3434
/// Converts this User instance to JSON data.
3535
Map<String, dynamic> toJson() => _$UserToJson(this);

lib/src/models/user.g.dart

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/models/user_role.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
/// Defines the available user roles in the system.
4+
@JsonEnum(fieldRename: FieldRename.snake)
5+
enum UserRole {
6+
/// Administrator role with full privileges.
7+
admin,
8+
9+
/// Standard user role with regular privileges.
10+
standardUser,
11+
12+
/// Guest user role with limited privileges.
13+
guestUser,
14+
}

test/src/models/auth_success_response_test.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:ht_shared/ht_shared.dart';
2+
import 'package:ht_shared/src/models/user_role.dart';
23
import 'package:test/test.dart';
34

45
void main() {
@@ -7,7 +8,7 @@ void main() {
78
const testUser = User(
89
id: 'user-123',
910
email: 'test@example.com',
10-
role: Role(name: 'standard_user'),
11+
role: UserRole.standardUser,
1112
);
1213
const testToken = 'sample-jwt-token';
1314

@@ -94,7 +95,7 @@ void main() {
9495
const updatedUser = User(
9596
id: 'user-456',
9697
email: 'updated@example.com',
97-
role: Role(name: 'guest_user'), // Use Role object
98+
role: UserRole.guestUser,
9899
);
99100
final copiedResponse = authSuccessResponse.copyWith(user: updatedUser);
100101

@@ -115,7 +116,7 @@ void main() {
115116
});
116117

117118
test('should create a copy with both user and token updated', () {
118-
const updatedUser = User(id: 'user-789', role: Role(name: 'guest_user')); // Use Role object
119+
const updatedUser = User(id: 'user-789', role: UserRole.guestUser);
119120
const updatedToken = 'another-token-xyz';
120121
final copiedResponse = authSuccessResponse.copyWith(
121122
user: updatedUser,
@@ -137,7 +138,7 @@ void main() {
137138
});
138139

139140
test('should not equate instances with different users', () {
140-
const differentUser = User(id: 'diff-user', role: Role(name: 'admin')); // Use Role object
141+
const differentUser = User(id: 'diff-user', role: UserRole.admin);
141142
const response1 = AuthSuccessResponse(user: testUser, token: testToken);
142143
const response2 = AuthSuccessResponse(
143144
user: differentUser,

test/src/models/role_test.dart

Lines changed: 0 additions & 39 deletions
This file was deleted.

test/src/models/user_test.dart

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import 'package:ht_shared/src/models/role.dart';
21
import 'package:ht_shared/src/models/user.dart';
2+
import 'package:ht_shared/src/models/user_role.dart';
33
import 'package:test/test.dart';
44

55
void main() {
66
group('User Model', () {
77
const id = 'test-id';
88
const email = 'test@example.com';
9-
const standardRole = Role(name: 'standard_user');
10-
const guestRole = Role(name: 'guest_user');
11-
const adminRole = Role(name: 'admin');
9+
const standardRole = UserRole.standardUser;
10+
const guestRole = UserRole.guestUser;
11+
const adminRole = UserRole.admin;
1212

1313
test('supports value equality', () {
1414
expect(
@@ -18,20 +18,14 @@ void main() {
1818
expect(
1919
const User(id: id, email: email, role: standardRole),
2020
isNot(
21-
equals(
22-
const User(id: 'other-id', email: email, role: standardRole),
23-
),
21+
equals(const User(id: 'other-id', email: email, role: standardRole)),
2422
),
2523
);
2624
expect(
2725
const User(id: id, email: email, role: standardRole),
2826
isNot(
2927
equals(
30-
const User(
31-
id: id,
32-
email: 'other@example.com',
33-
role: standardRole,
34-
),
28+
const User(id: id, email: 'other@example.com', role: standardRole),
3529
),
3630
),
3731
);
@@ -48,15 +42,15 @@ void main() {
4842
test('has correct toString', () {
4943
expect(
5044
const User(id: id, email: email, role: standardRole).toString(),
51-
equals('User(id: $id, email: $email, role: ${standardRole.toString()})'),
45+
equals('User(id: $id, email: $email, role: $standardRole)'),
5246
);
5347
expect(
5448
const User(id: id, role: guestRole).toString(),
55-
equals('User(id: $id, email: null, role: ${guestRole.toString()})'),
49+
equals('User(id: $id, email: null, role: $guestRole)'),
5650
);
5751
expect(
5852
const User(id: id, role: adminRole).toString(),
59-
equals('User(id: $id, email: null, role: ${adminRole.toString()})'),
53+
equals('User(id: $id, email: null, role: $adminRole)'),
6054
);
6155
});
6256

0 commit comments

Comments
 (0)