implemented user service (no auth!)
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
part 'user_dto.freezed.dart';
|
||||
part 'user_dto.g.dart';
|
||||
|
||||
@freezed
|
||||
class UserDto with _$UserDto {
|
||||
const factory UserDto({
|
||||
String? id,
|
||||
String? name,
|
||||
String? email,
|
||||
String? password,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt,
|
||||
}) = _UserDto;
|
||||
|
||||
/// JSON-Serialisierung
|
||||
factory UserDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$UserDtoFromJson(json);
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
// coverage:ignore-file
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
|
||||
|
||||
part of 'user_dto.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// FreezedGenerator
|
||||
// **************************************************************************
|
||||
|
||||
T _$identity<T>(T value) => value;
|
||||
|
||||
final _privateConstructorUsedError = UnsupportedError(
|
||||
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
|
||||
|
||||
UserDto _$UserDtoFromJson(Map<String, dynamic> json) {
|
||||
return _UserDto.fromJson(json);
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
mixin _$UserDto {
|
||||
String? get id => throw _privateConstructorUsedError;
|
||||
String? get name => throw _privateConstructorUsedError;
|
||||
String? get email => throw _privateConstructorUsedError;
|
||||
String? get password => throw _privateConstructorUsedError;
|
||||
DateTime? get createdAt => throw _privateConstructorUsedError;
|
||||
DateTime? get updatedAt => throw _privateConstructorUsedError;
|
||||
|
||||
/// Serializes this UserDto to a JSON map.
|
||||
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
|
||||
|
||||
/// Create a copy of UserDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
$UserDtoCopyWith<UserDto> get copyWith => throw _privateConstructorUsedError;
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class $UserDtoCopyWith<$Res> {
|
||||
factory $UserDtoCopyWith(UserDto value, $Res Function(UserDto) then) =
|
||||
_$UserDtoCopyWithImpl<$Res, UserDto>;
|
||||
@useResult
|
||||
$Res call(
|
||||
{String? id,
|
||||
String? name,
|
||||
String? email,
|
||||
String? password,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class _$UserDtoCopyWithImpl<$Res, $Val extends UserDto>
|
||||
implements $UserDtoCopyWith<$Res> {
|
||||
_$UserDtoCopyWithImpl(this._value, this._then);
|
||||
|
||||
// ignore: unused_field
|
||||
final $Val _value;
|
||||
// ignore: unused_field
|
||||
final $Res Function($Val) _then;
|
||||
|
||||
/// Create a copy of UserDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? id = freezed,
|
||||
Object? name = freezed,
|
||||
Object? email = freezed,
|
||||
Object? password = freezed,
|
||||
Object? createdAt = freezed,
|
||||
Object? updatedAt = freezed,
|
||||
}) {
|
||||
return _then(_value.copyWith(
|
||||
id: freezed == id
|
||||
? _value.id
|
||||
: id // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
name: freezed == name
|
||||
? _value.name
|
||||
: name // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
email: freezed == email
|
||||
? _value.email
|
||||
: email // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
password: freezed == password
|
||||
? _value.password
|
||||
: password // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
createdAt: freezed == createdAt
|
||||
? _value.createdAt
|
||||
: createdAt // ignore: cast_nullable_to_non_nullable
|
||||
as DateTime?,
|
||||
updatedAt: freezed == updatedAt
|
||||
? _value.updatedAt
|
||||
: updatedAt // ignore: cast_nullable_to_non_nullable
|
||||
as DateTime?,
|
||||
) as $Val);
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class _$$UserDtoImplCopyWith<$Res> implements $UserDtoCopyWith<$Res> {
|
||||
factory _$$UserDtoImplCopyWith(
|
||||
_$UserDtoImpl value, $Res Function(_$UserDtoImpl) then) =
|
||||
__$$UserDtoImplCopyWithImpl<$Res>;
|
||||
@override
|
||||
@useResult
|
||||
$Res call(
|
||||
{String? id,
|
||||
String? name,
|
||||
String? email,
|
||||
String? password,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class __$$UserDtoImplCopyWithImpl<$Res>
|
||||
extends _$UserDtoCopyWithImpl<$Res, _$UserDtoImpl>
|
||||
implements _$$UserDtoImplCopyWith<$Res> {
|
||||
__$$UserDtoImplCopyWithImpl(
|
||||
_$UserDtoImpl _value, $Res Function(_$UserDtoImpl) _then)
|
||||
: super(_value, _then);
|
||||
|
||||
/// Create a copy of UserDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? id = freezed,
|
||||
Object? name = freezed,
|
||||
Object? email = freezed,
|
||||
Object? password = freezed,
|
||||
Object? createdAt = freezed,
|
||||
Object? updatedAt = freezed,
|
||||
}) {
|
||||
return _then(_$UserDtoImpl(
|
||||
id: freezed == id
|
||||
? _value.id
|
||||
: id // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
name: freezed == name
|
||||
? _value.name
|
||||
: name // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
email: freezed == email
|
||||
? _value.email
|
||||
: email // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
password: freezed == password
|
||||
? _value.password
|
||||
: password // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
createdAt: freezed == createdAt
|
||||
? _value.createdAt
|
||||
: createdAt // ignore: cast_nullable_to_non_nullable
|
||||
as DateTime?,
|
||||
updatedAt: freezed == updatedAt
|
||||
? _value.updatedAt
|
||||
: updatedAt // ignore: cast_nullable_to_non_nullable
|
||||
as DateTime?,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@JsonSerializable()
|
||||
class _$UserDtoImpl implements _UserDto {
|
||||
const _$UserDtoImpl(
|
||||
{this.id,
|
||||
this.name,
|
||||
this.email,
|
||||
this.password,
|
||||
this.createdAt,
|
||||
this.updatedAt});
|
||||
|
||||
factory _$UserDtoImpl.fromJson(Map<String, dynamic> json) =>
|
||||
_$$UserDtoImplFromJson(json);
|
||||
|
||||
@override
|
||||
final String? id;
|
||||
@override
|
||||
final String? name;
|
||||
@override
|
||||
final String? email;
|
||||
@override
|
||||
final String? password;
|
||||
@override
|
||||
final DateTime? createdAt;
|
||||
@override
|
||||
final DateTime? updatedAt;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'UserDto(id: $id, name: $name, email: $email, password: $password, createdAt: $createdAt, updatedAt: $updatedAt)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) ||
|
||||
(other.runtimeType == runtimeType &&
|
||||
other is _$UserDtoImpl &&
|
||||
(identical(other.id, id) || other.id == id) &&
|
||||
(identical(other.name, name) || other.name == name) &&
|
||||
(identical(other.email, email) || other.email == email) &&
|
||||
(identical(other.password, password) ||
|
||||
other.password == password) &&
|
||||
(identical(other.createdAt, createdAt) ||
|
||||
other.createdAt == createdAt) &&
|
||||
(identical(other.updatedAt, updatedAt) ||
|
||||
other.updatedAt == updatedAt));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
int get hashCode =>
|
||||
Object.hash(runtimeType, id, name, email, password, createdAt, updatedAt);
|
||||
|
||||
/// Create a copy of UserDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
@pragma('vm:prefer-inline')
|
||||
_$$UserDtoImplCopyWith<_$UserDtoImpl> get copyWith =>
|
||||
__$$UserDtoImplCopyWithImpl<_$UserDtoImpl>(this, _$identity);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return _$$UserDtoImplToJson(
|
||||
this,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _UserDto implements UserDto {
|
||||
const factory _UserDto(
|
||||
{final String? id,
|
||||
final String? name,
|
||||
final String? email,
|
||||
final String? password,
|
||||
final DateTime? createdAt,
|
||||
final DateTime? updatedAt}) = _$UserDtoImpl;
|
||||
|
||||
factory _UserDto.fromJson(Map<String, dynamic> json) = _$UserDtoImpl.fromJson;
|
||||
|
||||
@override
|
||||
String? get id;
|
||||
@override
|
||||
String? get name;
|
||||
@override
|
||||
String? get email;
|
||||
@override
|
||||
String? get password;
|
||||
@override
|
||||
DateTime? get createdAt;
|
||||
@override
|
||||
DateTime? get updatedAt;
|
||||
|
||||
/// Create a copy of UserDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@override
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
_$$UserDtoImplCopyWith<_$UserDtoImpl> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'user_dto.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
_$UserDtoImpl _$$UserDtoImplFromJson(Map<String, dynamic> json) =>
|
||||
_$UserDtoImpl(
|
||||
id: json['id'] as String?,
|
||||
name: json['name'] as String?,
|
||||
email: json['email'] as String?,
|
||||
password: json['password'] as String?,
|
||||
createdAt: json['createdAt'] == null
|
||||
? null
|
||||
: DateTime.parse(json['createdAt'] as String),
|
||||
updatedAt: json['updatedAt'] == null
|
||||
? null
|
||||
: DateTime.parse(json['updatedAt'] as String),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$$UserDtoImplToJson(_$UserDtoImpl instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'name': instance.name,
|
||||
'email': instance.email,
|
||||
'password': instance.password,
|
||||
'createdAt': instance.createdAt?.toIso8601String(),
|
||||
'updatedAt': instance.updatedAt?.toIso8601String(),
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'package:backend_dart/application/service/dto/user_dto.dart';
|
||||
import 'package:backend_dart/domain/entities/user.dart';
|
||||
import 'package:backend_dart/domain/interface/error.dart';
|
||||
import 'package:backend_dart/domain/interface/mapper.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
|
||||
class UserDtoMapper implements IMapper<User, UserDto> {
|
||||
@override
|
||||
TaskEither<IError, UserDto> to(User entity) => TaskEither.of(UserDto(
|
||||
id: entity.id,
|
||||
name: entity.name,
|
||||
email: entity.email,
|
||||
createdAt: entity.createdAt,
|
||||
updatedAt: entity.updatedAt,
|
||||
));
|
||||
@override
|
||||
TaskEither<IError, User> from(UserDto dto) => TaskEither.of(User(
|
||||
id: dto.id,
|
||||
name: dto.name,
|
||||
email: dto.email,
|
||||
password: dto.password,
|
||||
createdAt: dto.createdAt,
|
||||
updatedAt: dto.updatedAt,
|
||||
));
|
||||
|
||||
@override
|
||||
TaskEither<IError, List<User>> listFrom(Iterable<UserDto> targets) {
|
||||
return TaskEither.traverseList(targets.toList(), from);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<IError, List<UserDto>> listTo(Iterable<User> origins) {
|
||||
return TaskEither.traverseList(origins.toList(), to);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:backend_dart/application/service/dto/user_dto.dart';
|
||||
import 'package:backend_dart/application/service/mapper/user_dto_mapper.dart';
|
||||
import 'package:backend_dart/common/response_helpers.dart';
|
||||
import 'package:backend_dart/domain/errors/app_error.dart';
|
||||
import 'package:backend_dart/domain/interface/error.dart';
|
||||
import 'package:backend_dart/domain/repository/user_repository.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf_router/shelf_router.dart';
|
||||
|
||||
part 'user_service.g.dart'; // generated with 'pub run build_runner build'
|
||||
|
||||
class UserService {
|
||||
final UserRepository users;
|
||||
final UserDtoMapper mapper = UserDtoMapper();
|
||||
UserService(this.users);
|
||||
|
||||
@Route.get('/')
|
||||
Future<Response> listUsers(Request request) async {
|
||||
return users
|
||||
.findAll()
|
||||
.flatMap(mapper.listTo)
|
||||
.map((users) => users.map((user) => user.toJson()).toList())
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok(jsonEncode(right)))
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.get('/<userId>')
|
||||
Future<Response> fetchUser(Request request, String userId) async {
|
||||
return users
|
||||
.findById(userId)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.post('/')
|
||||
Future<Response> createUser(Request request) async {
|
||||
return TaskEither<IError, UserDto>.tryCatch(() async {
|
||||
final json = jsonDecode(await request.readAsString());
|
||||
final user = UserDto.fromJson(json);
|
||||
return user;
|
||||
}, (error, stack) {
|
||||
return AppError.inputError(
|
||||
message: 'Failed to create user: ${error.toString()}');
|
||||
})
|
||||
.flatMap(mapper.from)
|
||||
.flatMap(users.create)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.put('/<userId>')
|
||||
Future<Response> updateUser(Request request, String userId) async {
|
||||
return TaskEither<IError, UserDto>.tryCatch(() async {
|
||||
final json = jsonDecode(await request.readAsString());
|
||||
final user = UserDto.fromJson(json);
|
||||
return user;
|
||||
}, (error, stack) {
|
||||
return AppError.inputError(
|
||||
message: 'Failed to update user: ${error.toString()}');
|
||||
})
|
||||
.flatMap(mapper.from)
|
||||
.flatMap(users.update)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.delete('/<userId>')
|
||||
Future<Response> deleteUser(Request request, String userId) async {
|
||||
return users
|
||||
.delete(userId)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok('user deleted'))
|
||||
.run();
|
||||
}
|
||||
|
||||
// Create router using the generate function defined in 'userservice.g.dart'.
|
||||
Router get router => _$UserServiceRouter(this);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'user_service.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// ShelfRouterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
Router _$UserServiceRouter(UserService service) {
|
||||
final router = Router();
|
||||
router.add(
|
||||
'GET',
|
||||
r'/',
|
||||
service.listUsers,
|
||||
);
|
||||
router.add(
|
||||
'GET',
|
||||
r'/<userId>',
|
||||
service.fetchUser,
|
||||
);
|
||||
router.add(
|
||||
'POST',
|
||||
r'/',
|
||||
service.createUser,
|
||||
);
|
||||
router.add(
|
||||
'PUT',
|
||||
r'/<userId>',
|
||||
service.updateUser,
|
||||
);
|
||||
router.add(
|
||||
'DELETE',
|
||||
r'/<userId>',
|
||||
service.deleteUser,
|
||||
);
|
||||
return router;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import 'package:backend_dart/application/repository/provider.dart';
|
||||
import 'package:backend_dart/application/service/user_service.dart';
|
||||
import 'package:riverpod/riverpod.dart';
|
||||
|
||||
final userServiceProvider = Provider<UserService>((ref) {
|
||||
final database = ref.read(userRepoProvider);
|
||||
return UserService(database);
|
||||
});
|
||||
Reference in New Issue
Block a user