made use of first class citizen objects as json validator functions
This commit is contained in:
parent
2210fe4bb1
commit
aac9c8af4f
@ -27,7 +27,7 @@ class ProjectRepositoryImpl implements ProjectRepository {
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<IError, void> delete(String id) {
|
||||
TaskEither<IError, Project> delete(String id) {
|
||||
return database.projects.delete(id);
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ class ProjectTaskRepositoryImpl implements ProjectTaskRepository {
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<IError, void> delete(String id) {
|
||||
TaskEither<IError, ProjectTask> delete(String id) {
|
||||
return database.tasks.delete(id);
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ class TimeEntryRepositoryImpl implements TimeEntryRepository {
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<IError, void> delete(String id) {
|
||||
TaskEither<IError, TimeEntry> delete(String id) {
|
||||
return database.timeEntries.delete(id);
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ class UserRepositoryImpl implements UserRepository {
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<IError, void> delete(String id) {
|
||||
TaskEither<IError, User> delete(String id) {
|
||||
return database.users.delete(id);
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,8 @@ class UserDto with _$UserDto {
|
||||
}) = _UserDto;
|
||||
|
||||
/// JSON-Serialisierung
|
||||
factory UserDto.fromJson(Map<String, dynamic> json) => _$UserDtoFromJson(json);
|
||||
factory UserDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$UserDtoFromJson(json);
|
||||
}
|
||||
|
||||
@freezed
|
||||
|
@ -22,8 +22,8 @@ class ProjectService {
|
||||
.findAll()
|
||||
.flatMap(mapper.listTo)
|
||||
.map((projects) => projects.map((project) => project.toJson()).toList())
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok(jsonEncode(right)))
|
||||
.map(jsonEncode)
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@ -32,35 +32,34 @@ class ProjectService {
|
||||
return projects
|
||||
.findById(projectId)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.post('/')
|
||||
Future<Response> createProject(Request request) async {
|
||||
return requestToJson(request)
|
||||
.flatMap((json) =>
|
||||
validateJsonKeys(json, ['name', 'userId'])) // Add required fields
|
||||
.flatMap(validateJsonKeys(['name', 'userId'])) // Add required fields
|
||||
.flatMap((json) => decodeJson(json, ProjectCreateDto.fromJson))
|
||||
.flatMap(mapper.fromCreateTo)
|
||||
.flatMap(projects.create)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.put('/<projectId>')
|
||||
Future<Response> updateProject(Request request, String projectId) async {
|
||||
return requestToJson(request)
|
||||
.flatMap((json) => validateJsonKeys(json, []))
|
||||
.flatMap(validateJsonKeys([]))
|
||||
.flatMap((json) => decodeJson(json, ProjectUpdateDto.fromJson))
|
||||
.flatMap((dto) => mapper.fromUpdateTo(dto, projectId))
|
||||
.flatMap(projects.update)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@ -68,8 +67,9 @@ class ProjectService {
|
||||
Future<Response> deleteProject(Request request, String projectId) async {
|
||||
return projects
|
||||
.delete(projectId)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok('project deleted'))
|
||||
.flatMap(mapper.to)
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
|
@ -22,8 +22,8 @@ class ProjectTaskService {
|
||||
.findAll()
|
||||
.flatMap(mapper.listTo)
|
||||
.map((tasks) => tasks.map((task) => task.toJson()).toList())
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok(jsonEncode(right)))
|
||||
.map(jsonEncode)
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@ -32,35 +32,34 @@ class ProjectTaskService {
|
||||
return tasks
|
||||
.findById(taskId)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.post('/')
|
||||
Future<Response> createTask(Request request) async {
|
||||
return requestToJson(request)
|
||||
.flatMap((json) =>
|
||||
validateJsonKeys(json, ['name', 'projectId'])) // Add required fields
|
||||
.flatMap(validateJsonKeys(['name', 'projectId'])) // Add required fields
|
||||
.flatMap((json) => decodeJson(json, ProjectTaskCreateDto.fromJson))
|
||||
.flatMap(mapper.fromCreateTo)
|
||||
.flatMap(tasks.create)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.put('/<taskId>')
|
||||
Future<Response> updateTask(Request request, String taskId) async {
|
||||
return requestToJson(request)
|
||||
.flatMap((json) => validateJsonKeys(json, []))
|
||||
.flatMap(validateJsonKeys([]))
|
||||
.flatMap((json) => decodeJson(json, ProjectTaskUpdateDto.fromJson))
|
||||
.flatMap((dto) => mapper.fromUpdateTo(dto, taskId))
|
||||
.flatMap(tasks.update)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@ -68,8 +67,9 @@ class ProjectTaskService {
|
||||
Future<Response> deleteTask(Request request, String taskId) async {
|
||||
return tasks
|
||||
.delete(taskId)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok('Task deleted'))
|
||||
.flatMap(mapper.to)
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
|
@ -22,8 +22,8 @@ class TimeEntryService {
|
||||
.findAll()
|
||||
.flatMap(mapper.listTo)
|
||||
.map((entries) => entries.map((entry) => entry.toJson()).toList())
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok(jsonEncode(right)))
|
||||
.map(jsonEncode)
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@ -32,35 +32,35 @@ class TimeEntryService {
|
||||
return entries
|
||||
.findById(entryId)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.post('/')
|
||||
Future<Response> createEntry(Request request) async {
|
||||
return requestToJson(request)
|
||||
.flatMap((json) => validateJsonKeys(
|
||||
json, ['startTime', 'endTime', 'userId', 'projectId']))
|
||||
.flatMap(
|
||||
validateJsonKeys(['startTime', 'endTime', 'userId', 'projectId']))
|
||||
.flatMap((json) => decodeJson(json, TimeEntryCreateDto.fromJson))
|
||||
.flatMap(mapper.fromCreateTo)
|
||||
.flatMap(entries.create)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.put('/<entryId>')
|
||||
Future<Response> updateEntry(Request request, String entryId) async {
|
||||
return requestToJson(request)
|
||||
.flatMap((json) => validateJsonKeys(json, []))
|
||||
.flatMap(validateJsonKeys([]))
|
||||
.flatMap((json) => decodeJson(json, TimeEntryUpdateDto.fromJson))
|
||||
.flatMap((dto) => mapper.fromUpdateTo(dto, entryId))
|
||||
.flatMap(entries.update)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@ -68,8 +68,8 @@ class TimeEntryService {
|
||||
Future<Response> deleteEntry(Request request, String entryId) async {
|
||||
return entries
|
||||
.delete(entryId)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok('Entry deleted'))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
|
@ -22,8 +22,8 @@ class UserService {
|
||||
.findAll()
|
||||
.flatMap(mapper.listTo)
|
||||
.map((users) => users.map((user) => user.toJson()).toList())
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok(jsonEncode(right)))
|
||||
.map(jsonEncode)
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@ -32,35 +32,34 @@ class UserService {
|
||||
return users
|
||||
.findById(userId)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.post('/')
|
||||
Future<Response> createUser(Request request) async {
|
||||
return requestToJson(request)
|
||||
.flatMap(
|
||||
(json) => validateJsonKeys(json, ['name', 'email', 'password']))
|
||||
.flatMap(validateJsonKeys(['name', 'email', 'password']))
|
||||
.flatMap((json) => decodeJson(json, UserCreateDto.fromJson))
|
||||
.flatMap(mapper.fromCreateTo)
|
||||
.flatMap(users.create)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@Route.put('/<userId>')
|
||||
Future<Response> updateUser(Request request, String userId) async {
|
||||
return requestToJson(request)
|
||||
.flatMap((json) => validateJsonKeys(json, []))
|
||||
.flatMap(validateJsonKeys([]))
|
||||
.flatMap((json) => decodeJson(json, UserUpdateDto.fromJson))
|
||||
.flatMap((dto) => mapper.fromUpdateTo(dto, userId))
|
||||
.flatMap(users.update)
|
||||
.flatMap(mapper.to)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right.toJson()))
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
@ -68,8 +67,9 @@ class UserService {
|
||||
Future<Response> deleteUser(Request request, String userId) async {
|
||||
return users
|
||||
.delete(userId)
|
||||
.match((left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok('user deleted'))
|
||||
.flatMap(mapper.to)
|
||||
.map((dto) => dto.toJson())
|
||||
.toResponse()
|
||||
.run();
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,24 @@
|
||||
import 'dart:convert';
|
||||
import 'package:backend_dart/domain/errors/error_code.dart';
|
||||
import 'package:backend_dart/domain/interface/error.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:shelf/shelf.dart';
|
||||
|
||||
extension TaskEitherResponseExtensions
|
||||
on TaskEither<IError, Map<String, dynamic>> {
|
||||
Task<Response> toResponse() => match(
|
||||
(left) => ResponseHelpers.fromError(left),
|
||||
(right) => ResponseHelpers.jsonOk(right),
|
||||
);
|
||||
}
|
||||
|
||||
extension TaskEitherResponseExtensionsFromString on TaskEither<IError, String> {
|
||||
Task<Response> toResponse() => match(
|
||||
(left) => ResponseHelpers.fromError(left),
|
||||
(right) => Response.ok(right),
|
||||
);
|
||||
}
|
||||
|
||||
class ResponseHelpers {
|
||||
/// Sendet eine JSON-Antwort mit einem 200-Statuscode
|
||||
static Response jsonOk(Map<String, dynamic> data) {
|
||||
|
@ -1,22 +1,18 @@
|
||||
import 'package:backend_dart/domain/errors/app_error.dart';
|
||||
import 'package:backend_dart/domain/interface/error.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
|
||||
TaskEither<IError, Map<String, dynamic>> validateJsonKeys(
|
||||
Map<String, dynamic> json, List<String> requiredKeys) {
|
||||
return TaskEither.tryCatch(
|
||||
() async {
|
||||
TaskEither<AppError, Map<String, dynamic>> Function(Map<String, dynamic>)
|
||||
validateJsonKeys(List<String> requiredKeys) {
|
||||
return (json) {
|
||||
final missingKeys =
|
||||
requiredKeys.where((key) => !json.containsKey(key)).toList();
|
||||
|
||||
if (missingKeys.isNotEmpty) {
|
||||
throw Exception('Missing required keys: ${missingKeys.join(', ')}');
|
||||
return TaskEither.left(AppError.validationError(
|
||||
message: 'Missing required keys: ${missingKeys.join(', ')}',
|
||||
));
|
||||
}
|
||||
|
||||
return json;
|
||||
},
|
||||
(error, _) => AppError.validationError(
|
||||
message: 'Failed to validate JSON keys: ${error.toString()}',
|
||||
),
|
||||
);
|
||||
return TaskEither.right(json);
|
||||
};
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ abstract class UserDataSource {
|
||||
|
||||
TaskEither<IError, User> update(UserUpdate user);
|
||||
|
||||
TaskEither<IError, void> delete(String id);
|
||||
TaskEither<IError, User> delete(String id);
|
||||
|
||||
TaskEither<IError, List<User>> findAll();
|
||||
|
||||
|
@ -13,7 +13,7 @@ abstract class ProjectRepository {
|
||||
TaskEither<IError, Project> update(ProjectUpdate project);
|
||||
|
||||
/// Deletes a project by its unique ID.
|
||||
TaskEither<IError, void> delete(String id);
|
||||
TaskEither<IError, Project> delete(String id);
|
||||
|
||||
/// Finds all projects.
|
||||
TaskEither<IError, List<Project>> findAll();
|
||||
|
@ -16,7 +16,7 @@ abstract class ProjectTaskRepository {
|
||||
TaskEither<IError, ProjectTask> update(ProjectTaskUpdate task);
|
||||
|
||||
/// Deletes a project task by its unique ID.
|
||||
TaskEither<IError, void> delete(String id);
|
||||
TaskEither<IError, ProjectTask> delete(String id);
|
||||
|
||||
/// Finds all project tasks.
|
||||
TaskEither<IError, List<ProjectTask>> findAll();
|
||||
|
@ -19,7 +19,7 @@ abstract class TimeEntryRepository {
|
||||
TaskEither<IError, TimeEntry> update(TimeEntryUpdate timeEntry);
|
||||
|
||||
/// Deletes a time entry by its unique ID.
|
||||
TaskEither<IError, void> delete(String id);
|
||||
TaskEither<IError, TimeEntry> delete(String id);
|
||||
|
||||
/// Finds all time entries.
|
||||
TaskEither<IError, List<TimeEntry>> findAll();
|
||||
|
@ -11,7 +11,7 @@ abstract class UserRepository {
|
||||
|
||||
TaskEither<IError, User> update(UserUpdate user);
|
||||
|
||||
TaskEither<IError, void> delete(String id);
|
||||
TaskEither<IError, User> delete(String id);
|
||||
|
||||
TaskEither<IError, List<User>> findAll();
|
||||
}
|
||||
|
@ -45,9 +45,13 @@ class PrismaUserDataSource implements UserDataSource {
|
||||
message: 'Failed to find user by email: ${error.toString()}',
|
||||
),
|
||||
)
|
||||
.flatMap(errorOnNull(AppError.notFound(
|
||||
.flatMap(
|
||||
errorOnNull(
|
||||
AppError.notFound(
|
||||
'User with email $email found',
|
||||
)))
|
||||
),
|
||||
),
|
||||
)
|
||||
.flatMap(mapper.from);
|
||||
}
|
||||
|
||||
@ -63,9 +67,13 @@ class PrismaUserDataSource implements UserDataSource {
|
||||
message: 'Failed to find user by ID: ${error.toString()}',
|
||||
),
|
||||
)
|
||||
.flatMap(errorOnNull(AppError.notFound(
|
||||
.flatMap(
|
||||
errorOnNull(
|
||||
AppError.notFound(
|
||||
"User with id $id not found",
|
||||
)))
|
||||
),
|
||||
),
|
||||
)
|
||||
.flatMap(mapper.from);
|
||||
}
|
||||
|
||||
@ -104,15 +112,24 @@ class PrismaUserDataSource implements UserDataSource {
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<IError, void> delete(String id) {
|
||||
return TaskEither.tryCatch(
|
||||
TaskEither<IError, User> delete(String id) {
|
||||
return TaskEither<IError, UserDbo?>.tryCatch(
|
||||
() async {
|
||||
await prisma.userDbo.delete(where: UserDboWhereUniqueInput(id: id));
|
||||
return await prisma.userDbo
|
||||
.delete(where: UserDboWhereUniqueInput(id: id));
|
||||
},
|
||||
(error, _) => AppError.databaseError(
|
||||
message: 'Failed to delete user: ${error.toString()}',
|
||||
),
|
||||
);
|
||||
)
|
||||
.flatMap(
|
||||
errorOnNull(
|
||||
AppError.notFound(
|
||||
'User not found',
|
||||
),
|
||||
),
|
||||
)
|
||||
.flatMap(mapper.from);
|
||||
}
|
||||
|
||||
@override
|
||||
|
Loading…
x
Reference in New Issue
Block a user