| 1 | // Event Tracker System - Refactored Solution |
| 2 | // Original: next-edit-4-2.ts |
| 3 | // Refactored to follow SOLID principles |
| 4 | |
| 5 | // Types |
| 6 | type EventCategory = "meeting" | "conference" | "workshop" | "other"; |
| 7 | |
| 8 | interface Event { |
| 9 | id: number; |
| 10 | name: string; |
| 11 | date: Date; |
| 12 | attendees: string[]; |
| 13 | location?: string; |
| 14 | category?: EventCategory; |
| 15 | } |
| 16 | |
| 17 | // EventRepository - Handles data storage and retrieval |
| 18 | class EventRepository { |
| 19 | private events: Event[] = []; |
| 20 | private nextEventId = 1; |
| 21 | |
| 22 | getAll(): Event[] { |
| 23 | return [...this.events]; |
| 24 | } |
| 25 | |
| 26 | getById(eventId: number): Event | undefined { |
| 27 | return this.events.find((event) => event.id === eventId); |
| 28 | } |
| 29 | |
| 30 | add(event: Omit<Event, "id">): number { |
| 31 | const newEvent: Event = { |
| 32 | ...event, |
| 33 | id: this.nextEventId++, |
| 34 | }; |
| 35 | |
| 36 | this.events.push(newEvent); |
| 37 | return newEvent.id; |
| 38 | } |
| 39 | |
| 40 | update(eventId: number, eventUpdates: Partial<Omit<Event, "id">>): boolean { |
| 41 | const eventIndex = this.findEventIndex(eventId); |
| 42 | if (eventIndex === -1) return false; |
| 43 | |
| 44 | this.events[eventIndex] = { |
| 45 | ...this.events[eventIndex], |
| 46 | ...eventUpdates, |
| 47 | }; |
| 48 | |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | remove(eventId: number): boolean { |
| 53 | const eventIndex = this.findEventIndex(eventId); |
| 54 | if (eventIndex === -1) return false; |
| 55 | |
| 56 | this.events.splice(eventIndex, 1); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | findByDateRange(startDate: Date, endDate: Date): Event[] { |
| 61 | return this.events.filter( |
| 62 | (event) => event.date >= startDate && event.date <= endDate, |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | findByCategory(category: EventCategory): Event[] { |
| 67 | return this.events.filter((event) => event.category === category); |
| 68 | } |
| 69 | |
| 70 | private findEventIndex(eventId: number): number { |
| 71 | return this.events.findIndex((event) => event.id === eventId); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // EventService - Handles business logic |
| 76 | class EventService { |
| 77 | constructor(private repository: EventRepository) {} |
| 78 | |
| 79 | createEvent( |
| 80 | name: string, |
| 81 | date: Date, |
| 82 | attendees: string[], |
| 83 | location?: string, |
| 84 | category?: EventCategory, |
| 85 | ): number { |
| 86 | return this.repository.add({ |
| 87 | name, |
| 88 | date, |
| 89 | attendees, |
| 90 | location, |
| 91 | category, |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | updateEvent(eventId: number, updates: Partial<Omit<Event, "id">>): boolean { |
| 96 | return this.repository.update(eventId, updates); |
| 97 | } |
| 98 | |
| 99 | removeEvent(eventId: number): boolean { |
| 100 | return this.repository.remove(eventId); |
| 101 | } |
| 102 | |
| 103 | addAttendee(eventId: number, attendeeName: string): boolean { |
| 104 | const event = this.repository.getById(eventId); |
| 105 | if (!event) return false; |
| 106 | |
| 107 | // Don't add if already exists |
| 108 | if (event.attendees.includes(attendeeName)) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | return this.repository.update(eventId, { |
| 113 | attendees: [...event.attendees, attendeeName], |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | removeAttendee(eventId: number, attendeeName: string): boolean { |
| 118 | const event = this.repository.getById(eventId); |
| 119 | if (!event) return false; |
| 120 | |
| 121 | const initialCount = event.attendees.length; |
| 122 | const updatedAttendees = event.attendees.filter( |
| 123 | (name) => name !== attendeeName, |
| 124 | ); |
| 125 | |
| 126 | if (initialCount === updatedAttendees.length) { |
| 127 | return false; // Attendee wasn't in the list |
| 128 | } |
| 129 | |
| 130 | return this.repository.update(eventId, { |
| 131 | attendees: updatedAttendees, |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | getEventsByDateRange(startDate: Date, endDate: Date): Event[] { |
| 136 | return this.repository.findByDateRange(startDate, endDate); |
| 137 | } |
| 138 | |
| 139 | getEventsByCategory(category: EventCategory): Event[] { |
| 140 | return this.repository.findByCategory(category); |
| 141 | } |
| 142 | |
| 143 | getAllEvents(): Event[] { |
| 144 | return this.repository.getAll(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Create instances for use |
| 149 | const eventRepository = new EventRepository(); |
| 150 | const eventService = new EventService(eventRepository); |
| 151 | |
| 152 | // Example usage |
| 153 | eventService.createEvent( |
| 154 | "Team Meeting", |
| 155 | new Date("2023-06-15T10:00:00"), |
| 156 | ["Alice", "Bob", "Charlie"], |
| 157 | "Conference Room A", |
| 158 | "meeting", |
| 159 | ); |
| 160 | |
| 161 | eventService.createEvent( |
| 162 | "JavaScript Workshop", |
| 163 | new Date("2023-06-20T14:00:00"), |
| 164 | ["David", "Eve"], |
| 165 | "Training Center", |
| 166 | "workshop", |
| 167 | ); |
| 168 | |
| 169 | console.log("All events:", eventService.getAllEvents()); |
| 170 | eventService.addAttendee(1, "Frank"); |
| 171 | console.log("Events after adding Frank:", eventService.getAllEvents()); |
| 172 | |
| 173 | // Export functions with the same interface as the original for backward compatibility |
| 174 | export const events = eventService.getAllEvents(); |
| 175 | |
| 176 | export function add_event( |
| 177 | eventName: string, |
| 178 | eventDate: Date, |
| 179 | attendees: string[], |
| 180 | location?: string, |
| 181 | category?: EventCategory, |
| 182 | ): number { |
| 183 | return eventService.createEvent( |
| 184 | eventName, |
| 185 | eventDate, |
| 186 | attendees, |
| 187 | location, |
| 188 | category, |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | export function removeEvent(eventId: number): boolean { |
| 193 | return eventService.removeEvent(eventId); |
| 194 | } |
| 195 | |
| 196 | export function updateEvent( |
| 197 | eventId: number, |
| 198 | name?: string, |
| 199 | date?: Date, |
| 200 | attendees?: string[], |
| 201 | location?: string, |
| 202 | category?: EventCategory, |
| 203 | ): boolean { |
| 204 | return eventService.updateEvent(eventId, { |
| 205 | name, |
| 206 | date, |
| 207 | attendees, |
| 208 | location, |
| 209 | category, |
| 210 | }); |
| 211 | } |
| 212 | |
| 213 | export function addAttendeeToEvent( |
| 214 | eventId: number, |
| 215 | attendeeName: string, |
| 216 | ): boolean { |
| 217 | return eventService.addAttendee(eventId, attendeeName); |
| 218 | } |
| 219 | |
| 220 | export function removeAttendeeFromEvent( |
| 221 | eventId: number, |
| 222 | attendeeName: string, |
| 223 | ): boolean { |
| 224 | return eventService.removeAttendee(eventId, attendeeName); |
| 225 | } |
| 226 | |
| 227 | export function getEventsByDateRange(startDate: Date, endDate: Date): Event[] { |
| 228 | return eventService.getEventsByDateRange(startDate, endDate); |
| 229 | } |
| 230 | |
| 231 | export function getEventsByCategory(category: EventCategory): Event[] { |
| 232 | return eventService.getEventsByCategory(category); |
| 233 | } |
| 234 | |
| 235 | // For direct use of the service in new code |
| 236 | export { Event, EventCategory, EventRepository, EventService }; |
| 237 | |
| 238 | /* |
| 239 | Code Smells in next-edit-4-2.ts: |
| 240 | Violation of Single Responsibility Principle: |
| 241 | |
| 242 | The code mixes data storage, business logic, and usage in one flat file |
| 243 | Global state management with events array and nextEventId variable |
| 244 | Poor Encapsulation: |
| 245 | |
| 246 | Direct manipulation of the global events array |
| 247 | No protection against external modification of data |
| 248 | Inconsistent Naming Conventions: |
| 249 | |
| 250 | Mix of snake_case (add_event) and camelCase (removeEvent) |
| 251 | Lack of consistent function naming patterns |
| 252 | Duplicated Logic: |
| 253 | |
| 254 | Repeated event lookup code in multiple functions |
| 255 | Multiple loops to find events by ID |
| 256 | Inefficient Algorithms: |
| 257 | |
| 258 | Linear searches through arrays instead of using more efficient methods |
| 259 | Manual implementation of array operations that have built-in methods |
| 260 | No Separation of Concerns: |
| 261 | |
| 262 | No distinction between data access and business logic |
| 263 | No clear architecture or organization |
| 264 | Mutable Shared State: |
| 265 | |
| 266 | Direct modification of the global events array |
| 267 | No immutability principles |
| 268 | |
| 269 | |
| 270 | Improvements Made: |
| 271 | |
| 272 | Applied SOLID Principles: |
| 273 | |
| 274 | Single Responsibility: Split into EventRepository (data storage) and EventService (business logic) |
| 275 | Open/Closed: Better structure for extending functionality without modifying existing code |
| 276 | Interface Segregation: Cleaner interfaces with focused responsibilities |
| 277 | Dependency Inversion: Service depends on repository abstraction |
| 278 | |
| 279 | |
| 280 | Improved Encapsulation: |
| 281 | |
| 282 | Added proper classes with private fields |
| 283 | Controlled access to data through methods |
| 284 | Immutable return values (using spreads and filters) |
| 285 | |
| 286 | |
| 287 | Consistent Naming: |
| 288 | |
| 289 | All methods follow camelCase convention |
| 290 | More descriptive and consistent naming patterns |
| 291 | |
| 292 | |
| 293 | Removed Duplication: |
| 294 | |
| 295 | Centralized event lookup in the repository |
| 296 | Reused code through proper method calls |
| 297 | |
| 298 | |
| 299 | More Efficient Algorithms: |
| 300 | |
| 301 | Used built-in array methods (find, filter, etc.) instead of manual loops |
| 302 | Simplified attendee management logic |
| 303 | |
| 304 | |
| 305 | Clear Separation of Concerns: |
| 306 | |
| 307 | Repository handles data access |
| 308 | Service handles business logic |
| 309 | Maintained backward compatibility with original exports |
| 310 | |
| 311 | |
| 312 | Better Type Definitions: |
| 313 | |
| 314 | Extracted EventCategory type |
| 315 | Used TypeScript features like Partial and Omit for more precise typing |
| 316 | Added proper interfaces |
| 317 | |
| 318 | |
| 319 | Maintained Backward Compatibility: |
| 320 | |
| 321 | Kept original function exports to ensure existing code can work |
| 322 | Added additional exports for new usage patterns |
| 323 | */ |