33using TodoListBlazorWasm . Api . Repositories ;
44using TodoListBlazorWasm . Models . Requests . Task ;
55using TodoListBlazorWasm . Models . Responses ;
6+ using YANLib ;
67using static System . DateTime ;
78using static TodoListBlazorWasm . Models . Enums . Status ;
89
@@ -12,12 +13,12 @@ namespace TodoListBlazorWasm.Api.Controllers;
1213[ ApiController ]
1314public sealed class TasksController : ControllerBase
1415{
15- private readonly ITaskRepository _taskRepository ;
16+ private readonly ITaskRepository _repository ;
1617
17- public TasksController ( ITaskRepository taskRepository ) => _taskRepository = taskRepository ;
18+ public TasksController ( ITaskRepository repository ) => _repository = repository ;
1819
1920 [ HttpGet ]
20- public async ValueTask < IActionResult > GetAll ( ) => Ok ( ( await _taskRepository . GetAll ( ) ) . Select ( x => new TaskResponse
21+ public async ValueTask < IActionResult > GetAll ( ) => Ok ( ( await _repository . GetAll ( ) ) . Select ( x => new TaskResponse
2122 {
2223 Id = x . Id ,
2324 Name = x . Name ,
@@ -36,7 +37,7 @@ public sealed class TasksController : ControllerBase
3637 [ HttpGet ( "{id}" ) ]
3738 public async ValueTask < IActionResult > Get ( Guid id )
3839 {
39- var ent = await _taskRepository . Get ( id ) ;
40+ var ent = await _repository . Get ( id ) ;
4041
4142 return Ok ( ent is null ? default : new TaskResponse
4243 {
@@ -46,7 +47,7 @@ public async ValueTask<IActionResult> Get(Guid id)
4647 Status = ent . Status ,
4748 CreatedAt = ent . CreatedAt ,
4849 UpdatedAt = ent . UpdatedAt ,
49- Assignee = ent . Assignee is null ? null : new UserResponse
50+ Assignee = ent . Assignee is null ? default : new UserResponse
5051 {
5152 Id = ent . Assignee . Id ,
5253 FirstName = ent . Assignee . FirstName ,
@@ -56,47 +57,99 @@ public async ValueTask<IActionResult> Get(Guid id)
5657 }
5758
5859 [ HttpPost ]
59- public async ValueTask < IActionResult > Create ( [ Required ] TaskCreateRequest request ) => ! ModelState . IsValid ? BadRequest ( ModelState ) : Ok ( await _taskRepository . Create ( new Entities . Task
60+ public async ValueTask < IActionResult > Create ( [ Required ] TaskCreateRequest request )
6061 {
61- Id = request . Id ,
62- Name = request . Name ,
63- AssigneeId = request . AssigneeId ,
64- Priority = request . Priority ,
65- Status = Open ,
66- CreatedAt = Now
67- } ) ) ;
62+ if ( ! ModelState . IsValid )
63+ {
64+ return BadRequest ( ModelState ) ;
65+ }
6866
69- [ HttpPut ( "{id}" ) ]
67+ var rslt = await _repository . Create ( new Entities . Task
68+ {
69+ Id = request . Id ,
70+ Name = request . Name ,
71+ AssigneeId = request . AssigneeId ,
72+ Priority = request . Priority ,
73+ Status = Open ,
74+ CreatedAt = Now
75+ } ) ;
76+
77+ return rslt is null ? Problem ( ) : Ok ( new TaskResponse
78+ {
79+ Id = rslt ! . Id ,
80+ Name = rslt . Name ,
81+ Priority = rslt . Priority ,
82+ Status = rslt . Status ,
83+ CreatedAt = rslt . CreatedAt ,
84+ UpdatedAt = rslt . UpdatedAt ,
85+ Assignee = rslt . Assignee is null ? default : new UserResponse
86+ {
87+ Id = rslt . Assignee . Id ,
88+ FirstName = rslt . Assignee . FirstName ,
89+ LastName = rslt . Assignee . LastName ,
90+ }
91+ } ) ;
92+ }
93+
94+ [ HttpPatch ( "{id}" ) ]
7095 public async ValueTask < IActionResult > Update ( Guid id , [ Required ] TaskUpdateRequest request )
7196 {
7297 if ( ! ModelState . IsValid )
7398 {
7499 return BadRequest ( ModelState ) ;
75100 }
76- else
101+
102+ var ent = await _repository . Get ( id ) ;
103+
104+ if ( ent is null )
105+ {
106+ return NotFound ( $ "{ id } is not found!") ;
107+ }
108+
109+ if ( request . Name ! . IsNotWhiteSpaceAndNull ( ) )
110+ {
111+ ent . Name = request . Name ! ;
112+ }
113+
114+ if ( request . AssigneeId . HasValue )
77115 {
78- var ent = await _taskRepository . Get ( id ) ;
79-
80- return ent is null
81- ? NotFound ( $ "{ id } is not found!")
82- : Ok ( await _taskRepository . Update ( new Entities . Task
83- {
84- Id = id ,
85- Name = request . Name ,
86- AssigneeId = request . AssigneeId ,
87- Priority = request . Priority ,
88- Status = request . Status ,
89- CreatedAt = ent . CreatedAt ,
90- UpdatedAt = Now
91- } ) ) ;
116+ ent . AssigneeId = request . AssigneeId . Value ;
92117 }
118+
119+ if ( request . Priority . HasValue )
120+ {
121+ ent . Priority = request . Priority . Value ;
122+ }
123+
124+ if ( request . Status . HasValue )
125+ {
126+ ent . Status = request . Status . Value ;
127+ }
128+
129+ var rslt = await _repository . Update ( ent ) ;
130+
131+ return rslt is null ? Problem ( ) : Ok ( new TaskResponse
132+ {
133+ Id = rslt . Id ,
134+ Name = rslt . Name ,
135+ Priority = rslt . Priority ,
136+ Status = rslt . Status ,
137+ CreatedAt = rslt . CreatedAt ,
138+ UpdatedAt = rslt . UpdatedAt ,
139+ Assignee = rslt . Assignee is null ? default : new UserResponse
140+ {
141+ Id = rslt . Assignee . Id ,
142+ FirstName = rslt . Assignee . FirstName ,
143+ LastName = rslt . Assignee . LastName ,
144+ }
145+ } ) ;
93146 }
94147
95148 [ HttpDelete ( "{id}" ) ]
96149 public async ValueTask < IActionResult > Delete ( Guid id )
97150 {
98- var ent = await _taskRepository . Get ( id ) ;
151+ var ent = await _repository . Get ( id ) ;
99152
100- return ent is null ? NotFound ( $ "{ id } is not found!") : Ok ( await _taskRepository . Delete ( ent ) ) ;
153+ return ent is null ? NotFound ( $ "{ id } is not found!") : Ok ( await _repository . Delete ( ent ) ) ;
101154 }
102155}
0 commit comments