source: resTools_backend/backend/Services/ToDoService.cs

Last change on this file was a569b7c, checked in by Danilo <danilo.najkov@…>, 2 years ago

todo items full functionality

  • Property mode set to 100644
File size: 2.9 KB
Line 
1using backend.Data;
2using backend.DTOs;
3using backend.Entities;
4using Microsoft.EntityFrameworkCore;
5
6namespace backend.Services
7{
8 public interface IToDoService
9 {
10 Task AddNewToDoItem(CreateToDoItemRequest item);
11 Task<List<ToDoItemResponse>> GetToDoItems(string sortBy);
12 Task UpdateToDoItemStatus(int id, ToDoStatus status);
13 }
14 public class ToDoService : IToDoService
15 {
16 private readonly DataContext _context = null;
17 public ToDoService(DataContext context)
18 {
19 _context = context;
20 }
21 public async Task AddNewToDoItem(CreateToDoItemRequest item)
22 {
23
24 var linkedReview = await _context.Reviews.FirstOrDefaultAsync(x => x.Id == item.ReviewId);
25
26 var newItem = new ToDoItem()
27 {
28 Description = item.Description,
29 CreatedAt = DateTime.UtcNow,
30 Status = ToDoStatus.New,
31 Title = item.Title,
32 LinkedReview = linkedReview,
33 Priority = item.Priority
34 };
35 await _context.ToDoItems.AddAsync(newItem);
36 await _context.SaveChangesAsync();
37 }
38
39 public async Task<List<ToDoItemResponse>> GetToDoItems(string sortBy)
40 {
41 var items = await _context.ToDoItems
42 .Include(x => x.LinkedReview)
43 .Select(x => new ToDoItemResponse()
44 {
45 Description = x.Description,
46 CreatedAt = x.CreatedAt,
47 Id = x.Id,
48 LinkedReview = x.LinkedReview == null ? null : new ReviewResponse()
49 {
50 Description = x.LinkedReview.Description,
51 CreatedAt = x.LinkedReview.CreatedAt,
52 Id = x.LinkedReview.Id,
53 Stars = x.LinkedReview.Stars,
54 Title = x.LinkedReview.Title
55 },
56 Status = x.Status,
57 Title = x.Title,
58 Priority = x.Priority
59 })
60 .ToListAsync();
61
62 if (sortBy.Equals("date"))
63 {
64 items = items.OrderByDescending(x => x.Status == ToDoStatus.New || x.Status == ToDoStatus.InProgress).ThenByDescending(x => x.CreatedAt).ToList();
65 }
66 else
67 {
68 items = items.OrderByDescending(x => x.Status == ToDoStatus.New || x.Status == ToDoStatus.InProgress).ThenBy(x => x.Priority).ToList();
69 }
70
71 return items;
72 }
73
74 public async Task UpdateToDoItemStatus(int id, ToDoStatus status)
75 {
76 var item = await _context.ToDoItems.FirstOrDefaultAsync(x => x.Id == id);
77 item.Status = status;
78 _context.Update(item);
79 await _context.SaveChangesAsync();
80 }
81 }
82}
Note: See TracBrowser for help on using the repository browser.