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
|
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading.Tasks;
using Timeline.Filters;
using Timeline.Models.Http;
using Timeline.Models.Validation;
using Timeline.Services;
namespace Timeline.Controllers
{
[ApiController]
[CatchTimelineNotExistException]
public class PersonalTimelineController : Controller
{
private readonly ILogger<PersonalTimelineController> _logger;
private readonly IPersonalTimelineService _service;
public PersonalTimelineController(ILogger<PersonalTimelineController> logger, IPersonalTimelineService service)
{
_logger = logger;
_service = service;
}
[HttpGet("users/{username}/timeline")]
public async Task<ActionResult<TimelineInfo>> TimelineGet([FromRoute][Username] string username)
{
return (await _service.GetTimeline(username)).FillLinks(Url);
}
[HttpGet("users/{username}/timeline/posts")]
public async Task<ActionResult<IList<TimelinePostInfo>>> PostListGet([FromRoute][Username] string username)
{
if (!this.IsAdministrator() && !await _service.HasReadPermission(username, this.GetOptionalUserId()))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
return await _service.GetPosts(username);
}
[HttpPost("users/{username}/timeline/posts")]
[Authorize]
public async Task<ActionResult<TimelinePostInfo>> PostPost([FromRoute][Username] string username, [FromBody] TimelinePostCreateRequest body)
{
var id = this.GetUserId();
if (!this.IsAdministrator() && !await _service.IsMemberOf(username, id))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
var res = await _service.CreatePost(username, id, body.Content, body.Time);
return res;
}
[HttpDelete("users/{username}/timeline/posts/{id}")]
[Authorize]
public async Task<ActionResult> PostDelete([FromRoute][Username] string username, [FromRoute] long id)
{
try
{
if (!this.IsAdministrator() && !await _service.HasPostModifyPermission(username, id, this.GetUserId()))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
await _service.DeletePost(username, id);
return Ok(CommonDeleteResponse.Delete());
}
catch (TimelinePostNotExistException)
{
return Ok(CommonDeleteResponse.NotExist());
}
}
[HttpPatch("users/{username}/timeline")]
[Authorize]
public async Task<ActionResult<TimelineInfo>> TimelinePatch([FromRoute][Username] string username, [FromBody] TimelinePatchRequest body)
{
if (!this.IsAdministrator() && !(User.Identity.Name == username))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
await _service.ChangeProperty(username, body);
var timeline = (await _service.GetTimeline(username)).FillLinks(Url);
return Ok(timeline);
}
[HttpPut("users/{username}/timeline/members/{member}")]
[Authorize]
public async Task<ActionResult> TimelineMemberPut([FromRoute][Username] string username, [FromRoute][Username] string member)
{
if (!this.IsAdministrator() && !(User.Identity.Name == username))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
try
{
await _service.ChangeMember(username, new List<string> { member }, null);
return Ok();
}
catch (UserNotExistException)
{
return BadRequest(ErrorResponse.TimelineController.MemberPut_NotExist());
}
}
[HttpDelete("users/{username}/timeline/members/{member}")]
[Authorize]
public async Task<ActionResult> TimelineMemberDelete([FromRoute][Username] string username, [FromRoute][Username] string member)
{
if (!this.IsAdministrator() && !(User.Identity.Name == username))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
try
{
await _service.ChangeMember(username, null, new List<string> { member });
return Ok(CommonDeleteResponse.Delete());
}
catch (UserNotExistException)
{
return Ok(CommonDeleteResponse.NotExist());
}
}
}
}
|