@@ -21,6 +21,25 @@ public TopicsController(
21
21
_service = service ;
22
22
}
23
23
24
+ [ HttpPost ]
25
+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
26
+ public IActionResult PostTopic ( CreateTopicDto dtoModel )
27
+ {
28
+ if ( ! ModelState . IsValid ) return NotFound ( ) ;
29
+
30
+ var model = Mappers . DtoToModel ( dtoModel ) ;
31
+
32
+ var result = _service . Create ( model ) ;
33
+
34
+ if ( ! result . IsSuccess )
35
+ {
36
+ _logger . LogInformation ( $ " 🛑 Reason of 📧 exception is { result . exception ? . Message } ") ;
37
+ return BadRequest ( ) ;
38
+ }
39
+
40
+ return Created ( "/" , dtoModel ) ;
41
+ }
42
+
24
43
[ HttpGet ]
25
44
[ Produces ( "application/json" ) ]
26
45
[ ProducesResponseType ( StatusCodes . Status200OK , Type = typeof ( ResponseBase < List < Topic > > ) ) ]
@@ -42,29 +61,47 @@ public IActionResult GetTopics([FromQuery]Pagination pagination)
42
61
return Ok ( response ) ;
43
62
}
44
63
45
- [ HttpPost ]
46
- [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
47
- public async Task < IActionResult > PostTopic ( CreateTopicDto model )
64
+ [ HttpGet ( "{id}" ) ]
65
+ public IActionResult GetTopicById ( [ FromRoute ] ulong id )
48
66
{
49
- if ( ! ModelState . IsValid ) return NotFound ( ) ;
50
-
51
- var ModelgaAylanganDto = Mappers . DtoToModel ( model ) ;
52
-
53
- var result = await _service . CreateAsync ( ModelgaAylanganDto ) ;
54
- // Console.WriteLine($"Bu yerda malumot qoshilishi mumkin edi : {ModelgaAylanganDto}");
67
+ if ( ! _service . TopicExists ( id ) ) return NotFound ( "Topic not found" ) ;
55
68
56
-
69
+ var result = _service . GetById ( id ) ;
57
70
if ( ! result . IsSuccess )
58
71
{
59
- _logger . LogInformation ( $ "Error boldi sababi: { result . exception ? . Message } ") ;
72
+ _logger . LogInformation ( $ " 🛑 Reason of 📧 exception is { result . exception ? . Message } ") ;
60
73
return BadRequest ( ) ;
61
74
}
62
- // var entity = ToEntity(model);
75
+ return Ok ( result . topic ) ;
76
+ }
63
77
64
- // var result = await _unitOfWork.Topics.Add(entity);
65
- // _unitOfWork.Save();
78
+ [ HttpPut ( "{id}" ) ]
79
+ public IActionResult UpdateTopic ( [ FromRoute ] ulong id , [ FromForm ] UpdateTopicDto dtoModel )
80
+ {
81
+ if ( ! _service . TopicExists ( id ) ) return NotFound ( "Topic not found" ) ;
82
+
83
+ var model = Mappers . UpdateDtoToModel ( dtoModel ) ;
84
+ var result = _service . Update ( model ) ;
85
+ if ( ! result . IsSuccess )
86
+ {
87
+ _logger . LogInformation ( $ " 🛑 Reason of 📧 exception is { result . exception ? . Message } ") ;
88
+ return BadRequest ( ) ;
89
+ }
90
+ return Accepted ( result . topic ) ;
91
+ }
66
92
93
+ [ HttpDelete ( "{id}" ) ]
94
+ public IActionResult DeleteTopic ( [ FromQuery ] ulong id )
95
+ {
96
+ if ( ! _service . TopicExists ( id ) ) return NotFound ( "Topic not found" ) ;
67
97
68
- return Created ( "/" , model ) ;
98
+ var model = _service . GetById ( id ) ;
99
+ var result = _service . Remove ( model . topic ) ;
100
+ if ( ! result . IsSuccess )
101
+ {
102
+ _logger . LogInformation ( $ " 🛑 Reason of 📧 exception is { result . exception ? . Message } ") ;
103
+ return BadRequest ( ) ;
104
+ }
105
+ return Accepted ( result . topic ) ;
69
106
}
70
107
}
0 commit comments