-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathUploadRepository.cs
More file actions
86 lines (78 loc) · 2.52 KB
/
Copy pathUploadRepository.cs
File metadata and controls
86 lines (78 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Blazorcrud.Shared.Data;
using Blazorcrud.Shared.Models;
using Microsoft.EntityFrameworkCore;
namespace Blazorcrud.Server.Models
{
public class UploadRepository : IUploadRepository
{
private readonly AppDbContext _appDbContext;
public UploadRepository(AppDbContext appDbContext)
{
_appDbContext = appDbContext;
}
public async Task<Upload> AddUpload(Upload upload)
{
var result = await _appDbContext.Uploads.AddAsync(upload);
await _appDbContext.SaveChangesAsync();
return result.Entity;
}
public async Task<Upload?> DeleteUpload(int Id)
{
var result = await _appDbContext.Uploads.FirstOrDefaultAsync(u => u.Id==Id);
if (result!=null)
{
_appDbContext.Uploads.Remove(result);
await _appDbContext.SaveChangesAsync();
}
else
{
throw new KeyNotFoundException("Upload not found");
}
return result;
}
public async Task<Upload?> GetUpload(int Id)
{
var result = await _appDbContext.Uploads.FirstOrDefaultAsync(u => u.Id==Id);
if(result != null)
{
return result;
}
else
{
throw new KeyNotFoundException("Upload not found");
}
}
public PagedResult<Upload> GetUploads(string? name, int page)
{
int pageSize = 5;
if (name != null)
{
return _appDbContext.Uploads
.Where(u => u.FileName.Contains(name))
.OrderBy(u => u.UploadTimestamp)
.GetPaged(page, pageSize);
}
else
{
return _appDbContext.Uploads
.OrderBy(u => u.UploadTimestamp)
.GetPaged(page, pageSize);
}
}
public async Task<Upload?> UpdateUpload(Upload upload)
{
var result = await _appDbContext.Uploads.FirstOrDefaultAsync(u => u.Id==upload.Id);
if (result!=null)
{
// Update existing upload
_appDbContext.Entry(result).CurrentValues.SetValues(upload);
await _appDbContext.SaveChangesAsync();
}
else
{
throw new KeyNotFoundException("Upload not found");
}
return result;
}
}
}