1
+ using Microsoft . AspNetCore . Authorization ;
2
+ using Microsoft . AspNetCore . Mvc ;
3
+ using Microsoft . EntityFrameworkCore ;
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using System . Linq ;
7
+ using System . Security . Claims ;
8
+ using System . Threading . Tasks ;
9
+ using ToDoListProjeto . Api . Data ;
10
+ using ToDoListProjeto . Api . Models ;
11
+ using ToDoListProjeto . Api . Services ;
12
+
13
+ [ Authorize ]
14
+ [ ApiController ]
15
+ [ Route ( "api/tasks" ) ]
16
+ public class TasksController : ControllerBase
17
+ {
18
+ private readonly ApplicationDbContext _dbContext ;
19
+ private readonly AIService _aiService ;
20
+ public TasksController ( ApplicationDbContext dbContext , AIService aiService )
21
+ {
22
+ _dbContext = dbContext ;
23
+ _aiService = aiService ;
24
+ }
25
+
26
+
27
+ [ HttpGet ]
28
+ public async Task < ActionResult < IEnumerable < TaskItem > > > GetTasks ( )
29
+ {
30
+ var userId = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
31
+ if ( string . IsNullOrEmpty ( userId ) ) return Unauthorized ( ) ;
32
+
33
+ return await _dbContext . TaskItems
34
+ . Where ( t => t . UserId == userId )
35
+ . ToListAsync ( ) ;
36
+ }
37
+
38
+
39
+ [ HttpGet ( "{id}" ) ]
40
+ public async Task < ActionResult < TaskItem > > GetTask ( int id )
41
+ {
42
+ var userId = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
43
+ if ( string . IsNullOrEmpty ( userId ) ) return Unauthorized ( ) ;
44
+
45
+ var taskItem = await _dbContext . TaskItems
46
+ . Where ( t => t . Id == id && t . UserId == userId )
47
+ . FirstOrDefaultAsync ( ) ;
48
+
49
+ if ( taskItem == null ) return NotFound ( ) ;
50
+
51
+ return taskItem ;
52
+ }
53
+
54
+ [ HttpPost ]
55
+ public async Task < ActionResult < TaskItem > > PostTask ( TaskCreateModel model )
56
+ {
57
+ var userId = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
58
+ if ( string . IsNullOrEmpty ( userId ) )
59
+ {
60
+ return Unauthorized ( ) ;
61
+ }
62
+
63
+ var newTaskItem = new TaskItem
64
+ {
65
+ Title = model . Title ,
66
+ Description = model . Description ,
67
+ Status = model . Status ,
68
+ Priority = model . Priority ,
69
+ UserId = userId ,
70
+ CreatedAt = DateTime . UtcNow
71
+ } ;
72
+
73
+ _dbContext . TaskItems . Add ( newTaskItem ) ;
74
+ await _dbContext . SaveChangesAsync ( ) ;
75
+
76
+ return CreatedAtAction ( nameof ( GetTask ) , new { id = newTaskItem . Id } , newTaskItem ) ;
77
+ }
78
+
79
+ // IA
80
+ [ HttpPost ( "smart-add" ) ]
81
+ public async Task < ActionResult < TaskItem > > PostSmartTask ( [ FromBody ] SmartTaskModel model )
82
+ {
83
+ var parsedTask = await _aiService . ParseTaskFromPrompt ( model . Prompt ) ;
84
+
85
+ if ( parsedTask == null || string . IsNullOrWhiteSpace ( parsedTask . Title ) )
86
+ {
87
+ return BadRequest ( new { message = "Não foi possível interpretar a tarefa a partir do texto fornecido." } ) ;
88
+ }
89
+
90
+ var userId = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
91
+ if ( string . IsNullOrEmpty ( userId ) ) return Unauthorized ( ) ;
92
+
93
+ var newTaskItem = new TaskItem
94
+ {
95
+ Title = parsedTask . Title ,
96
+ Description = parsedTask . Description ,
97
+ Status = parsedTask . Status ,
98
+ Priority = parsedTask . Priority ,
99
+ UserId = userId ,
100
+ CreatedAt = DateTime . UtcNow
101
+ } ;
102
+
103
+ _dbContext . TaskItems . Add ( newTaskItem ) ;
104
+ await _dbContext . SaveChangesAsync ( ) ;
105
+
106
+ return CreatedAtAction ( nameof ( GetTask ) , new { id = newTaskItem . Id } , newTaskItem ) ;
107
+ }
108
+
109
+
110
+ [ HttpPut ( "{id}" ) ]
111
+ public async Task < IActionResult > PutTask ( int id , TaskUpdateModel model )
112
+ {
113
+ var userId = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
114
+ if ( string . IsNullOrEmpty ( userId ) ) return Unauthorized ( ) ;
115
+
116
+ var existingTask = await _dbContext . TaskItems
117
+ . Where ( t => t . Id == id && t . UserId == userId )
118
+ . FirstOrDefaultAsync ( ) ;
119
+
120
+ if ( existingTask == null ) return NotFound ( ) ;
121
+
122
+ if ( model . Title != null ) { existingTask . Title = model . Title ; }
123
+ if ( model . Description != null ) { existingTask . Description = model . Description ; }
124
+ if ( model . Status != null )
125
+ {
126
+ existingTask . Status = model . Status ;
127
+ if ( model . Status == "Concluída" ) { existingTask . CompletedAt = DateTime . UtcNow ; }
128
+ else { existingTask . CompletedAt = null ; }
129
+ }
130
+ if ( model . Priority != null ) { existingTask . Priority = model . Priority ; }
131
+
132
+ await _dbContext . SaveChangesAsync ( ) ;
133
+ return NoContent ( ) ;
134
+ }
135
+
136
+ [ HttpDelete ( "{id}" ) ]
137
+ public async Task < IActionResult > DeleteTask ( int id )
138
+ {
139
+ var userId = User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
140
+ if ( string . IsNullOrEmpty ( userId ) ) return Unauthorized ( ) ;
141
+
142
+ var taskItem = await _dbContext . TaskItems
143
+ . Where ( t => t . Id == id && t . UserId == userId )
144
+ . FirstOrDefaultAsync ( ) ;
145
+
146
+ if ( taskItem == null ) return NotFound ( ) ;
147
+
148
+ _dbContext . TaskItems . Remove ( taskItem ) ;
149
+ await _dbContext . SaveChangesAsync ( ) ;
150
+
151
+ return NoContent ( ) ;
152
+ }
153
+
154
+ private bool TaskItemExists ( int id )
155
+ {
156
+ return _dbContext . TaskItems . Any ( e => e . Id == id ) ;
157
+ }
158
+ }
159
+
160
+ public class SmartTaskModel
161
+ {
162
+ public string Prompt { get ; set ; }
163
+ }
0 commit comments