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

Fix Models for immutables #17

Merged
merged 7 commits into from
Nov 10, 2024
Merged
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
18 changes: 18 additions & 0 deletions lib/data/models/authorities.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:equatable/equatable.dart';

Expand All @@ -18,6 +20,22 @@ class Authorities extends Equatable {
);
}

static Authorities? fromJson(Map<String, dynamic> json) {
var result = JsonMapper.fromMap<Authorities>(json);
if (result == null) {
return null;
}
return result;
}

static Authorities? fromJsonString(String json){
var result = JsonMapper.deserialize<Authorities>(jsonDecode(json));
if (result == null) {
return null;
}
return result;
}

@override
List<Object?> get props => [
name,
Expand Down
41 changes: 38 additions & 3 deletions lib/data/models/change_password.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
import 'dart:convert';

import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:equatable/equatable.dart';

@jsonSerializable
class PasswordChangeDTO {
class PasswordChangeDTO extends Equatable {
@JsonProperty(name: 'currentPassword')
final String? currentPassword;

@JsonProperty(name: 'newPassword')
final String? newPassword;

const PasswordChangeDTO({
this.currentPassword = '',
this.newPassword = '',
this.currentPassword,
this.newPassword,
});

PasswordChangeDTO copyWith({
String? currentPassword,
String? newPassword,
}) {
return PasswordChangeDTO(
currentPassword: currentPassword ?? this.currentPassword,
newPassword: newPassword ?? this.newPassword,
);
}

static PasswordChangeDTO? fromJson(Map<String, dynamic> json) {
var result = JsonMapper.fromMap<PasswordChangeDTO>(json);
if (result == null) {
return null;
}
return result;
}

static PasswordChangeDTO? fromJsonString(String json) {
var result = JsonMapper.deserialize<PasswordChangeDTO>(jsonDecode(json));
if (result == null) {
return null;
}
return result;
}

@override
List<Object?> get props => [currentPassword, newPassword];

@override
bool get stringify => true;
}
18 changes: 18 additions & 0 deletions lib/data/models/city.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:equatable/equatable.dart';

Expand Down Expand Up @@ -30,6 +32,22 @@ class City extends Equatable {
);
}

static City? fromJson(Map<String, dynamic> json) {
var result = JsonMapper.fromMap<City>(json);
if (result == null) {
return null;
}
return result;
}

static City? fromJsonString(String json) {
var result = JsonMapper.deserialize<City>(jsonDecode(json));
if (result == null) {
return null;
}
return result;
}

@override
List<Object?> get props => [
id,
Expand Down
18 changes: 18 additions & 0 deletions lib/data/models/customer.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:equatable/equatable.dart';

Expand Down Expand Up @@ -64,6 +66,22 @@ class Customer extends Equatable {
);
}

static Customer? fromJson(Map<String, dynamic> json) {
var result = JsonMapper.fromMap<Customer>(json);
if (result == null) {
return null;
}
return result;
}

static Customer? fromJsonString(String json){
var result = JsonMapper.deserialize<Customer>(jsonDecode(json));
if (result == null) {
return null;
}
return result;
}

@override
List<Object?> get props => [
id,
Expand Down
18 changes: 18 additions & 0 deletions lib/data/models/district.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:equatable/equatable.dart';

Expand Down Expand Up @@ -30,6 +32,22 @@ class District extends Equatable {
);
}

static District? fromJson(Map<String, dynamic> json) {
var result = JsonMapper.fromMap<District>(json);
if (result == null) {
return null;
}
return result;
}

static District? fromJsonString(String json){
var result = JsonMapper.deserialize<District>(jsonDecode(json));
if (result == null) {
return null;
}
return result;
}

@override
List<Object?> get props => [
id,
Expand Down
43 changes: 41 additions & 2 deletions lib/data/models/jwt_token.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
import 'dart:convert';

import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:equatable/equatable.dart';

@jsonSerializable
class JWTToken {
class JWTToken extends Equatable {
@JsonProperty(name: 'id_token')
String? idToken;
final String? idToken;

const JWTToken({
this.idToken,
});

JWTToken copyWith({
String? idToken,
}) {
return JWTToken(
idToken: idToken ?? this.idToken,
);
}

static JWTToken? fromJson(Map<String, dynamic> json) {
var result = JsonMapper.fromMap<JWTToken>(json);
if (result == null) {
return null;
}
return result;
}

static JWTToken? fromJsonString(String json) {
var result = JsonMapper.deserialize<JWTToken>(jsonDecode(json));
if (result == null) {
return null;
}
return result;
}

@override
String toString() {
Expand All @@ -15,4 +46,12 @@ class JWTToken {

@override
int get hashCode => idToken.hashCode;

@override
List<Object?> get props => [
idToken,
];

@override
bool get stringify => true;
}
18 changes: 18 additions & 0 deletions lib/data/models/menu.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:equatable/equatable.dart';

Expand Down Expand Up @@ -80,6 +82,22 @@ class Menu extends Equatable {
);
}

static Menu? fromJson(Map<String, dynamic> json) {
var result = JsonMapper.fromMap<Menu>(json);
if (result == null) {
return null;
}
return result;
}

static Menu? fromJsonString(String json){
var result = JsonMapper.deserialize<Menu>(jsonDecode(json));
if (result == null) {
return null;
}
return result;
}

@override
List<Object?> get props => [
id,
Expand Down
35 changes: 31 additions & 4 deletions lib/data/models/user_jwt.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import 'dart:convert';

import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:equatable/equatable.dart';

@jsonSerializable
class UserJWT {
class UserJWT extends Equatable {
@JsonProperty(name: 'username')
String? username;
final String? username;

@JsonProperty(name: 'password')
String? password;
final String? password;

const UserJWT(this.username, this.password);

UserJWT copyWith({
String? username,
String? password,
}) {
return UserJWT(
username ?? this.username,
password ?? this.password,
);
}

UserJWT(this.username, this.password);
static UserJWT? fromJson(Map<String, dynamic> json) {
return JsonMapper.fromMap<UserJWT>(json);
}

static UserJWT? fromJsonString(String json) {
return JsonMapper.deserialize<UserJWT>(jsonDecode(json));
}

@override
String toString() {
Expand All @@ -21,4 +42,10 @@ class UserJWT {

@override
int get hashCode => username.hashCode ^ password.hashCode;

@override
List<Object?> get props => [username, password];

@override
bool get stringify => true;
}
10 changes: 5 additions & 5 deletions lib/data/repository/login_repository.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:flutter_bloc_advance/utils/storage.dart';
import 'package:shared_preferences/shared_preferences.dart';

Expand Down Expand Up @@ -28,13 +27,14 @@ class LoginRepository {
throw Exception("Invalid username or password");
}

final authenticateRequest = await HttpUtils.postRequest<UserJWT>("/authenticate", userJWT);
result = JsonMapper.deserialize<JWTToken>(authenticateRequest.body)!;
final response = await HttpUtils.postRequest<UserJWT>("/authenticate", userJWT);
result = JWTToken.fromJsonString(response.body);

if (result.idToken != null) {
if (result != null && result.idToken != null) {
saveStorage(jwtToken: result.idToken);
return result;
}
return result;
return JWTToken(idToken: null);
}

Future<void> logout() async {
Expand Down
2 changes: 1 addition & 1 deletion lib/main/main_local.mapper.g.dart

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/main/main_prod.mapper.g.dart

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions test/data/model/authority_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter_bloc_advance/data/models/authorities.dart';
import 'package:flutter_bloc_advance/main/main_local.mapper.g.dart';
import 'package:flutter_test/flutter_test.dart';

/// Test the Authorities model
void main() {
late Authorities authoritiesModel;
Authorities initAuthorities() {
return Authorities(
name: 'ROLE_USER',
);
}

setUp(() {
initializeJsonMapper();
authoritiesModel = initAuthorities();
});

group("Authorities Model", () {
test('should create a Authorities instance (Constructor)', () {
final finalAuthorities = authoritiesModel;

expect(finalAuthorities.name, 'ROLE_USER');
});

test('should copy a Authorities instance with new values (copyWith)', () {
final finalAuthorities = authoritiesModel;

final updatedAuthorities = finalAuthorities.copyWith(
name: 'ROLE_ADMIN',
);

expect(updatedAuthorities.name, 'ROLE_ADMIN');
});

test('should compare two Authorities instances', () {
final finalAuthorities = authoritiesModel;

final updatedAuthorities = finalAuthorities.copyWith(
name: 'ROLE_ADMIN',
);

expect(finalAuthorities == updatedAuthorities, false);
});
});
}
Loading
Loading