-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountController.cs
More file actions
187 lines (174 loc) · 6 KB
/
Copy pathAccountController.cs
File metadata and controls
187 lines (174 loc) · 6 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SplitzBackend.Models;
using SplitzBackend.Services;
namespace SplitzBackend.Controllers;
[Authorize]
[ApiController]
[Route("[controller]")]
public class AccountController(
SplitzDbContext db,
UserManager<SplitzUser> userManager,
IMapper mapper,
IImageStorageService imageStorage) : ControllerBase
{
/// <summary>
/// Get the current user's information
/// </summary>
/// <returns></returns>
[HttpGet(Name = "GetUserInfo")]
[Produces("application/json")]
[ProducesResponseType(200)]
[ProducesResponseType(401)]
public async Task<ActionResult<SplitzUserDto>> Get()
{
var user = await userManager.GetUserAsync(User);
if (user is null)
return Unauthorized();
user = await db.Users
.Include(u => u.Friends)
.ThenInclude(f => f.FriendUser)
.Include(u => u.Groups)
.Include(u => u.Balances)
.FirstAsync(u => u.Id == user.Id);
return mapper.Map<SplitzUserDto>(user);
}
/// <summary>
/// Update the current user's username and photo
/// </summary>
/// <param name="userDto">user info</param>
/// <returns></returns>
[HttpPatch(Name = "UpdateUserInfo")]
[ProducesResponseType(204)]
[ProducesResponseType(401)]
public async Task<ActionResult> Update(SplitzUserUpdateViewModel userDto)
{
var user = await userManager.GetUserAsync(User);
if (user is null)
return Unauthorized();
if (userDto.UserName is not null)
user.UserName = userDto.UserName;
await db.SaveChangesAsync();
return NoContent();
}
/// <summary>
/// Add a friend to the current user
/// </summary>
/// <param name="id">friend's id</param>
/// <param name="remark">friend's remark</param>
/// <returns></returns>
[HttpPost("friend/{id}", Name = "AddFriend")]
[ProducesResponseType(204)]
[ProducesResponseType(401)]
[ProducesResponseType(404)]
public async Task<ActionResult> AddFriend(string id, [FromBody] string? remark)
{
var user = await userManager.GetUserAsync(User);
if (user is null)
return Unauthorized();
var friend = await userManager.FindByIdAsync(id);
if (friend is null)
return NotFound();
if (user.Friends.Any(f => f.FriendUserId == friend.Id && f.UserId == user.Id))
return NoContent();
user.Friends.Add(new Friend
{
UserId = user.Id,
FriendUserId = friend.Id,
Remark = remark
});
await db.SaveChangesAsync();
return NoContent();
}
/// <summary>
/// Update the remark of a friend
/// </summary>
/// <param name="id">friend's id</param>
/// <param name="remark">friend's new remark</param>
/// <returns></returns>
[HttpPatch("friend/{id}", Name = "UpdateFriendRemark")]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(401)]
[ProducesResponseType(404)]
public async Task<ActionResult> UpdateFriend(string id, string? remark)
{
if (remark == null)
return BadRequest();
var user = await userManager.GetUserAsync(User);
if (user is null)
return Unauthorized();
var friend = await userManager.FindByIdAsync(id);
if (friend is null)
return NotFound();
var friendShip = user.Friends.FirstOrDefault(f => f.FriendUserId == friend.Id && f.UserId == user.Id);
if (friendShip is null)
return NotFound();
friendShip.Remark = remark;
await db.SaveChangesAsync();
return NoContent();
}
/// <summary>
/// Remove a friend from the current user
/// </summary>
/// <param name="id">friend's id</param>
/// <returns></returns>
[HttpDelete("friend/{id}", Name = "RemoveFriend")]
[ProducesResponseType(204)]
[ProducesResponseType(401)]
[ProducesResponseType(404)]
public async Task<ActionResult> RemoveFriend(string id)
{
var user = await userManager.GetUserAsync(User);
if (user is null)
return Unauthorized();
var friend = await userManager.FindByIdAsync(id);
if (friend is null)
return NotFound();
var friendShip = user.Friends.FirstOrDefault(f => f.FriendUserId == friend.Id && f.UserId == user.Id);
if (friendShip is null)
return NotFound();
user.Friends.Remove(friendShip);
await db.SaveChangesAsync();
return NoContent();
}
/// <summary>
/// Upload the current user's avatar image.
/// </summary>
[HttpPost("avatar", Name = "UploadUserAvatar")]
[Consumes("multipart/form-data")]
[Produces("application/json")]
[ProducesResponseType(401)]
[ProducesResponseType(400)]
[ProducesResponseType(200)]
[RequestSizeLimit(10 * 1024 * 1024)]
public async Task<ActionResult<UploadImageResult>> UploadAvatar(
IFormFile file,
CancellationToken cancellationToken = default)
{
var user = await userManager.GetUserAsync(User);
if (user is null)
return Unauthorized();
if (file.Length <= 0)
return BadRequest("Empty file");
var existingPhoto = user.Photo;
await using var input = file.OpenReadStream();
var result = await imageStorage.UploadProcessedImageAsync(
input,
file.ContentType,
$"users/{user.Id}/avatar",
new ImageResizeRequest(512, false),
cancellationToken);
user.Photo = result.ObjectKey;
await db.SaveChangesAsync(cancellationToken);
await imageStorage.DeleteIfOwnedAsync(existingPhoto, cancellationToken);
return Ok(result);
}
}
public class SplitzUserUpdateViewModel
{
public string? UserName { get; set; }
}