Skip to content

Commit

Permalink
feat: create new event
Browse files Browse the repository at this point in the history
  • Loading branch information
MikkyBoy357 committed Jan 14, 2025
1 parent 7103a79 commit dd7c856
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 47 deletions.
86 changes: 86 additions & 0 deletions lib/common/models/event_n_ticket/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Event {
DateTime date;
List<String> categories;
TicketType ticketType;
User createdBy;
DateTime createdAt;

Event({
required this.id,
Expand All @@ -20,6 +22,8 @@ class Event {
required this.date,
required this.categories,
required this.ticketType,
required this.createdBy,
required this.createdAt,
});

factory Event.fromJson(Map<String, dynamic> json) {
Expand All @@ -36,6 +40,12 @@ class Event {
ticketType: TicketType.fromJson(
json['ticketType'] as Map<String, dynamic>,
),
createdBy: User.fromJson(
json['createdBy'] as Map<String, dynamic>,
),
createdAt: json['createdAt'] != null
? DateTime.parse(json['createdAt'].toString())
: DateTime.now(),
);
}

Expand All @@ -49,6 +59,82 @@ class Event {
'date': date.toString(),
'categories': categories.map((e) => e).toList(),
'ticketType': ticketType.toJson(),
'createdBy': createdBy.toJson(),
'createdAt': createdAt.toString(),
};
}

EventRequest toEventRequest() {
return EventRequest(
name: name,
owner: owner,
description: description,
location: location,
date: date,
categories: categories,
ticketType: ticketType.id,
createdBy: createdBy.$_id,
createdAt: createdAt,
);
}
}

class EventRequest {
final String name;
final String owner;
final String description;
final String location;
final DateTime date;
final List<String> categories;
final ObjectId ticketType;
final ObjectId createdBy;
final DateTime createdAt;

EventRequest({
required this.name,
required this.owner,
required this.description,
required this.location,
required this.date,
required this.categories,
required this.ticketType,
required this.createdBy,
required this.createdAt,
});

factory EventRequest.fromJson(Map<String, dynamic> json) {
return EventRequest(
name: json['name'] as String,
owner: json['owner'] as String,
description: json['description'] as String,
location: json['location'] as String,
date: json['date'] != null
? DateTime.parse(json['date'].toString())
: DateTime.now(),
categories: (json['categories'] as List).map((e) => e as String).toList(),
ticketType: ObjectId.fromHexString(
json['ticketType'] as String,
),
createdBy: ObjectId.fromHexString(
json['createdBy'] as String,
),
createdAt: json['createdAt'] != null
? DateTime.parse(json['createdAt'].toString())
: DateTime.now(),
);
}

Map<String, dynamic> toJson() {
return {
'name': name,
'owner': owner,
'description': description,
'location': location,
'date': date.toString(),
'categories': categories,
'ticketType': ticketType,
'createdBy': createdBy,
'createdAt': createdAt.toString(),
};
}
}
10 changes: 0 additions & 10 deletions lib/common/models/event_n_ticket/ticket_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import 'package:mongo_dart/mongo_dart.dart';

class TicketType {
ObjectId id;
ObjectId eventId;
double price;
String name;
String description;

TicketType({
required this.id,
required this.eventId,
required this.price,
required this.name,
required this.description,
Expand All @@ -18,7 +16,6 @@ class TicketType {
factory TicketType.fromJson(Map<String, dynamic> json) {
return TicketType(
id: json['_id'] as ObjectId,
eventId: json['eventId'] as ObjectId,
price: json['price'] as double,
name: json['name'] as String,
description: json['description'] as String,
Expand All @@ -28,7 +25,6 @@ class TicketType {
Map<String, dynamic> toJson() {
return {
'_id': id,
'eventId': eventId,
'price': price,
'name': name,
'description': description,
Expand All @@ -39,7 +35,6 @@ class TicketType {
factory TicketType.sampleData() {
return TicketType(
id: ObjectId(),
eventId: ObjectId(),
price: 100,
name: 'VIP',
description: 'VIP ticket',
Expand All @@ -50,21 +45,18 @@ class TicketType {
// Request

class TicketTypeRequest {
String eventId;
double price;
String name;
String description;

TicketTypeRequest({
required this.eventId,
required this.price,
required this.name,
required this.description,
});

factory TicketTypeRequest.fromJson(Map<String, dynamic> json) {
return TicketTypeRequest(
eventId: json['eventId'].toString(),
price: json['price'] as double,
name: json['name'] as String,
description: json['description'] as String,
Expand All @@ -73,7 +65,6 @@ class TicketTypeRequest {

Map<String, dynamic> toJson() {
return {
'eventId': eventId,
'price': price,
'name': name,
'description': description,
Expand All @@ -83,7 +74,6 @@ class TicketTypeRequest {
TicketType toTicketType() {
return TicketType(
id: ObjectId(),
eventId: ObjectId.fromHexString(eventId),
price: price,
name: name,
description: description,
Expand Down
31 changes: 21 additions & 10 deletions lib/data/repositories/event_n_ticket/event_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:mongo_dart/mongo_dart.dart';

abstract class EventRepositoryImpl {
Future<List<Event>> getEvents();
Future<Event?> createEvent({required Event event});
Future<Event?> createEvent({required EventRequest eventRequest});
Future<Event?> getEventById({required ObjectId eventId});
Future<bool> deleteEvent({required ObjectId eventId});
}
Expand All @@ -20,11 +20,10 @@ class EventRepository extends EventRepositoryImpl {

@override
Future<List<Event>> getEvents() async {
final eventsCollectionExt = DbCollectionExt(_eventsCollection);

final res = await eventsCollectionExt.findAndPopulate(
final res = await _eventsCollection.findAndPopulateRikky(
[
PopulateField(fieldName: 'ticketType', collectionName: 'ticketTypes'),
PopulateField(fieldName: 'createdBy', collectionName: 'users'),
],
);
printGreen('Events: $res');
Expand All @@ -35,22 +34,34 @@ class EventRepository extends EventRepositoryImpl {
}

@override
Future<Event?> createEvent({required Event event}) async {
final result = await _eventsCollection.insertOne(event.toJson());
Future<Event?> createEvent({required EventRequest eventRequest}) async {
final result = await _eventsCollection.insertOne(eventRequest.toJson());
print('Create Event result: $result');

if (result.writeError != null) {
return null;
}

return event;
if (result.id is ObjectId) {
final eventId = result.id as ObjectId;
final event = await getEventById(eventId: eventId);
return event;
}

return null;
}

@override
Future<Event?> getEventById({required ObjectId eventId}) async {
final result = await _eventsCollection.findOne({
'_id': eventId,
});
final result = await _eventsCollection.findOneAndPopulateRikky(
{
'_id': eventId,
},
fieldsToPopulate: [
PopulateField(fieldName: 'ticketType', collectionName: 'ticketTypes'),
PopulateField(fieldName: 'createdBy', collectionName: 'users'),
],
);

if (result == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class TicketTypeRepository extends TicketTypeRepositoryImpl {

@override
Future<List<TicketType>> getTicketTypes() async {
final ticketTypesCollectionExt = DbCollectionExt(_ticketTypesCollection);

final res = await ticketTypesCollectionExt.findAndPopulate(
final res = await _ticketTypesCollection.findAndPopulateRikky(
[
// PopulateField(fieldName: 'eventId', collectionName: 'events'),
],
Expand All @@ -40,9 +38,7 @@ class TicketTypeRepository extends TicketTypeRepositoryImpl {
Future<List<TicketType>> getTicketTypesByEventId({
required ObjectId eventId,
}) async {
final ticketTypesCollectionExt = DbCollectionExt(_ticketTypesCollection);

final res = await ticketTypesCollectionExt.findAndPopulate(
final res = await _ticketTypesCollection.findAndPopulateRikky(
[
PopulateField(fieldName: 'eventId', collectionName: 'events'),
],
Expand Down
39 changes: 34 additions & 5 deletions lib/data/request_handlers/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ import 'package:mongo_dart/mongo_dart.dart';

abstract class EventRequestHandler {
Future<Response> handleGetAllEvents();
Future<Response> handleCreateEvent({required Event event});
Future<Response> handleCreateEvent({
required EventRequest eventRequest,
required User saint,
});
Future<Response> handleGetEventById({required ObjectId eventId});
Future<Response> handleDeleteEvent({required ObjectId eventId});
}

class EventRequestHandlerImpl implements EventRequestHandler {
final EventRepository _eventRepository;
final TicketTypeRepository _ticketTypeRepository;

const EventRequestHandlerImpl({
required EventRepository eventRepository,
}) : _eventRepository = eventRepository;
required TicketTypeRepository ticketTypeRepository,
}) : _eventRepository = eventRepository,
_ticketTypeRepository = ticketTypeRepository;

@override
Future<Response> handleGetAllEvents() async {
Expand All @@ -29,13 +35,36 @@ class EventRequestHandlerImpl implements EventRequestHandler {
}

@override
Future<Response> handleCreateEvent({required Event event}) async {
Future<Response> handleCreateEvent({
required EventRequest eventRequest,
required User saint,
}) async {
print('===> POST <==> Event:');
final createdEvent = await _eventRepository.createEvent(event: event);
final ticketType = await _ticketTypeRepository.getTicketTypeById(
ticketTypeId: eventRequest.ticketType,
);

if (ticketType == null) {
return Response.json(
body: 'Ticket Type with ID `${eventRequest.ticketType}` not found',
statusCode: 404,
);
}

final createdEvent = await _eventRepository.createEvent(
eventRequest: eventRequest,
);

if (createdEvent == null) {
return Response.json(
body: 'Failed to create event',
statusCode: 400,
);
}

return Response.json(
body: createdEvent,
statusCode: createdEvent != null ? 201 : 400,
statusCode: 201,
);
}

Expand Down
Loading

0 comments on commit dd7c856

Please sign in to comment.