Skip to content

Commit bebd742

Browse files
committed
feat: crud feedback
1 parent 9ffe7e5 commit bebd742

File tree

12 files changed

+407
-7
lines changed

12 files changed

+407
-7
lines changed
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Mvc;
4+
using MK.Domain.Dto.Request.Feedback;
5+
using MK.Domain.Dto.Response.Feedback;
6+
using MK.Service.Common;
7+
8+
namespace MK.API.Controllers
9+
{
10+
[Route("api/[controller]")]
11+
[ApiVersion("1.0")]
12+
[ApiController]
13+
public class FeedbackController : ControllerBase
14+
{
15+
private readonly IFeedbackService _feedbackService;
16+
public FeedbackController(IFeedbackService feedbackService)
17+
{
18+
_feedbackService = feedbackService;
19+
}
20+
21+
/// <summary>
22+
/// Function to create new Feedback
23+
/// </summary>
24+
/// <param name="req"></param>
25+
/// <returns>Guid of object have been created successfully</returns>
26+
/// <response code="200">Returns the newly created feedback</response>
27+
[HttpPost]
28+
[ProducesResponseType(typeof(Guid), StatusCodes.Status200OK)]
29+
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
30+
public async Task<IActionResult> Create([Required] CreateFeedbackReq req)
31+
{
32+
var result = await _feedbackService.Create(req);
33+
return StatusCode((int)result.StatusCode, result);
34+
}
35+
36+
/// <summary>
37+
/// Function to delete feedback
38+
/// </summary>
39+
/// <param name="feedbackId"></param>
40+
/// <returns></returns>
41+
/// <response code="200">Returns the deleted feedback</response>
42+
[HttpDelete("{feedbackId}")]
43+
[ProducesResponseType(typeof(Guid), StatusCodes.Status200OK)]
44+
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
45+
public async Task<IActionResult> Delete(Guid feedbackId)
46+
{
47+
var result = await _feedbackService.Delete(feedbackId);
48+
return StatusCode((int)result.StatusCode, result);
49+
}
50+
51+
/// <summary>
52+
/// Function to update feedback
53+
/// </summary>
54+
/// <param name="feedbackId"></param>
55+
/// <param name="req"></param>
56+
/// <returns></returns>
57+
[HttpPut("{feedbackId}")]
58+
[ProducesResponseType(typeof(Guid), StatusCodes.Status200OK)]
59+
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
60+
public async Task<IActionResult> Update(Guid feedbackId, [Required] UpdateFeedbackReq req)
61+
{
62+
var result = await _feedbackService.Update(feedbackId, req);
63+
return StatusCode((int)result.StatusCode, result);
64+
}
65+
66+
/// <summary>
67+
/// Function to get feedback by id
68+
/// </summary>
69+
/// <param name="feedbackId"></param>
70+
/// <returns></returns>
71+
/// <response code="200">Returns the feedback</response>
72+
/// <response code="400">If the feedback is null</response>
73+
[HttpGet("{feedbackId}")]
74+
[ProducesResponseType(typeof(FeedbackRes), StatusCodes.Status200OK)]
75+
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
76+
public async Task<IActionResult> GetById(Guid feedbackId)
77+
{
78+
var result = await _feedbackService.GetById(feedbackId);
79+
return StatusCode((int)result.StatusCode, result);
80+
}
81+
82+
/// <summary>
83+
/// Function to get all feedback with paging
84+
/// </summary>
85+
/// <param name="req"></param>
86+
/// <returns>
87+
/// Paging List of feedback response
88+
[HttpGet]
89+
[ProducesResponseType(typeof(PagingResponse<FeedbackRes>), StatusCodes.Status200OK)]
90+
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
91+
public async Task<IActionResult> GetAll([FromQuery] PagingParameters req, [FromQuery] string[]? fields)
92+
{
93+
if (fields != null && !fields.IsMatchFieldPattern())
94+
{
95+
return BadRequest(new ResponseObject<FeedbackRes>()
96+
{
97+
StatusCode = HttpStatusCode.BadRequest,
98+
Message = "Fields are not valid, they are not match with pattern [fieldName:action]"
99+
});
100+
}
101+
var result = await _feedbackService.GetAll(req, fields);
102+
return StatusCode((int)result.StatusCode, result);
103+
}
104+
105+
/// <summary>
106+
/// Function to get all feedback by kitchen id
107+
/// </summary>
108+
/// <param name="kitchenId"></param>
109+
/// <returns></returns>
110+
/// <response code="200">Returns the feedback</response>
111+
[HttpGet("kitchen/{kitchenId}")]
112+
[ProducesResponseType(typeof(PagingResponse<FeedbackRes>), StatusCodes.Status200OK)]
113+
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
114+
public async Task<IActionResult> GetByKitchenId([Required] Guid kitchenId, [FromQuery] PagingParameters req, [FromQuery] string[]? fields)
115+
{
116+
if (fields != null && !fields.IsMatchFieldPattern())
117+
{
118+
return BadRequest(new ResponseObject<FeedbackRes>()
119+
{
120+
StatusCode = HttpStatusCode.BadRequest,
121+
Message = "Fields are not valid, they are not match with pattern [fieldName:action]"
122+
});
123+
}
124+
var result = await _feedbackService.GetFeedbacksByKitchenId(kitchenId, req, fields);
125+
return StatusCode((int)result.StatusCode, result);
126+
}
127+
}
128+
}

MK.API/Controllers/KitchenController.cs

-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,5 @@ public async Task<IActionResult> GetByUserId([Required] Guid userId, [FromQuery]
169169
var result = await _kitchenService.GetKitchensByUserId(userId, req ?? new PagingParameters());
170170
return StatusCode((int)result.StatusCode, result);
171171
}
172-
173-
174172
}
175173
}

MK.Application/Repository/IUnitOfWork.cs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public interface IUnitOfWork
2424
IGenericRepository<PaymentType> PaymentType { get; }
2525
IGenericRepository<OrderPayment> OrderPayment { get; }
2626
IGenericRepository<Order> Order { get; }
27+
IGenericRepository<Feedback> Feedback { get; }
2728

2829
Task<int> SaveChangeAsync(CancellationToken cancellationToken = default);
2930
Task BeginTransactionAsync();
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using MK.Domain.Dto.Request.Feedback;
7+
using MK.Domain.Dto.Response.Feedback;
8+
9+
namespace MK.Application.Service
10+
{
11+
public interface IFeedbackService
12+
{
13+
Task<ResponseObject<Guid>> Create(CreateFeedbackReq req);
14+
Task<ResponseObject<bool>> Delete(Guid feedbackId);
15+
Task<ResponseObject<bool>> Update(Guid feedbackId, UpdateFeedbackReq req);
16+
Task<ResponseObject<FeedbackRes>> GetById(Guid feedbackId);
17+
Task<PagingResponse<FeedbackRes>> GetAll(PagingParameters pagingParam = null, string[] fields = null);
18+
Task<PagingResponse<FeedbackRes>> GetFeedbacksByKitchenId(Guid kitchenId, PagingParameters pagingParam = null, string[] fields = null);
19+
20+
21+
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace MK.Domain.Dto.Request.Feedback
9+
{
10+
public class CreateFeedbackReq
11+
{
12+
[Required]
13+
public string Content { get; set; } = null!;
14+
[Required]
15+
[Range(1, 5)]
16+
17+
public float Rating { get; set; }
18+
19+
public string? ImgUrl { get; set; }
20+
21+
public Guid CustomerId { get; set; }
22+
23+
public Guid OrderId { get; set; }
24+
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace MK.Domain.Dto.Request.Feedback
9+
{
10+
public class UpdateFeedbackReq
11+
{
12+
public string? Content { get; set; }
13+
[Range(1, 5)]
14+
15+
public float Rating { get; set; }
16+
17+
public string? ImgUrl { get; set; }
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.ComponentModel.DataAnnotations.Schema;
4-
using System.ComponentModel.DataAnnotations;
53
using System.Linq;
64
using System.Text;
75
using System.Threading.Tasks;
6+
using MK.Domain.Dto.Response.Customer;
87

98
namespace MK.Domain.Dto.Response.Feedback
109
{
1110
public class FeedbackRes
1211
{
13-
public string Content { get; set; }
12+
public int No { get; set; }
13+
public Guid Id { get; set; }
14+
public string Content { get; set; } = null!;
1415
public float Rating { get; set; }
15-
public string img_url { get; set; }
16+
public string? ImgUrl { get; set; }
17+
public Guid OwnerId { get; set; }
18+
public OwnerRes Owner { get; set; } = null!;
19+
public Guid OrderId { get; set; }
20+
1621
}
1722
}

MK.Domain/Entity/Feedback.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Feedback : BaseEntity
2020
public float Rating { get; set; }
2121

2222
[DataType(DataType.Text)]
23-
public string img_url { get; set; } = null!;
23+
public string? ImgUrl { get; set; }
2424

2525
[Required]
2626
public Guid CustomerId { get; set; }
@@ -30,5 +30,9 @@ public class Feedback : BaseEntity
3030
public Guid OrderId { get; set; }
3131
public virtual Order Order { get; set; } = null!;
3232

33+
[Required]
34+
public Guid KitchenId { get; set; }
35+
public virtual Kitchen Kitchen { get; set; } = null!;
36+
3337
}
3438
}

MK.Domain/Entity/Kitchen.cs

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ public partial class Kitchen : BaseEntity
3838
public virtual ICollection<Dish> Dishes { get; set; } = new List<Dish>();
3939
public virtual ICollection<Tray> Trays { get; set; } = new List<Tray>();
4040
public virtual ICollection<Meal> Meals { get; set; } = new List<Meal>();
41+
public virtual ICollection<Feedback> Feedbacks { get; set; } = new List<Feedback>();
4142
}
4243
}

MK.Infrastructure/DBContext/ApplicationDbContext.cs

+6
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
195195
.WithOne(r => r.Customer)
196196
.HasForeignKey(x => x.CustomerId)
197197
.IsRequired();
198+
modelBuilder.Entity<Kitchen>()
199+
.HasMany(x => x.Feedbacks)
200+
.WithOne(r => r.Kitchen)
201+
.HasForeignKey(x => x.KitchenId)
202+
.IsRequired();
203+
198204

199205
#endregion Config for Relationship
200206

MK.Infrastructure/Repository/UnitOfWork.cs

+13
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,19 @@ public IGenericRepository<OrderPayment> OrderPayment
218218
}
219219
}
220220

221+
public IGenericRepository<Feedback> _feedback;
222+
public IGenericRepository<Feedback> Feedback
223+
{
224+
get
225+
{
226+
if (_feedback == null)
227+
{
228+
_feedback = new GenericRepository<Feedback>(_dbContext);
229+
}
230+
return _feedback;
231+
}
232+
}
233+
221234

222235
#endregion Repository
223236

0 commit comments

Comments
 (0)