From a2cfc3d51b7be1943208901d810e3148e89ec2b3 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 5 Feb 2020 18:24:10 +0800 Subject: Improve relate filter. --- Timeline/Controllers/TimelineController.cs | 16 +++++++++--- Timeline/Services/TimelineService.cs | 40 +++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 10 deletions(-) (limited to 'Timeline') diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs index 811b0426..47b3696c 100644 --- a/Timeline/Controllers/TimelineController.cs +++ b/Timeline/Controllers/TimelineController.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using Timeline.Filters; using Timeline.Models.Http; @@ -28,14 +29,21 @@ namespace Timeline.Controllers } [HttpGet("timelines")] - public async Task>> TimelineList([FromQuery][Username] string? relate) + public async Task>> TimelineList([FromQuery][Username] string? relate, [FromQuery][RegularExpression("(own)|(join)")] string? relateType) { - long? relatedUserId = null; + TimelineUserRelationship? relationship = null; if (relate != null) { try { - relatedUserId = await _userService.GetUserIdByUsername(relate); + var relatedUserId = await _userService.GetUserIdByUsername(relate); + + relationship = new TimelineUserRelationship(relateType switch + { + "own" => TimelineUserRelationshipType.Own, + "join" => TimelineUserRelationshipType.Join, + _ => TimelineUserRelationshipType.Default + }, relatedUserId); } catch (UserNotExistException) { @@ -43,7 +51,7 @@ namespace Timeline.Controllers } } - var result = await _service.GetTimelines(relatedUserId); + var result = await _service.GetTimelines(relationship); result.ForEach(t => t.FillLinks(Url)); return Ok(result); } diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index 67a57deb..7afc0512 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -13,6 +13,25 @@ using static Timeline.Resources.Services.TimelineService; namespace Timeline.Services { + public enum TimelineUserRelationshipType + { + Own = 0b1, + Join = 0b10, + Default = Own | Join + } + + public class TimelineUserRelationship + { + public TimelineUserRelationship(TimelineUserRelationshipType type, long userId) + { + Type = type; + UserId = userId; + } + + public TimelineUserRelationshipType Type { get; set; } + public long UserId { get; set; } + } + /// /// This define the common interface of both personal timeline /// and normal timeline. @@ -231,12 +250,12 @@ namespace Timeline.Services /// /// Get all timelines including personal timelines. /// - /// Filter timelines related (own or is a member) to specific user. + /// Filter timelines related (own or is a member) to specific user. /// The list of timelines. /// /// If user with related user id does not exist, empty list will be returned. /// - Task> GetTimelines(long? relatedUserId = null); + Task> GetTimelines(TimelineUserRelationship? relate = null); /// /// Create a timeline. @@ -628,18 +647,27 @@ namespace Timeline.Services } } - public async Task> GetTimelines(long? relatedUserId = null) + public async Task> GetTimelines(TimelineUserRelationship? relate = null) { List entities; - if (relatedUserId == null) + if (relate == null) { entities = await Database.Timelines.Include(t => t.Members).ToListAsync(); } else { - var timelineAsMemberIds = await Database.TimelineMembers.Where(m => m.UserId == relatedUserId).Select(m => m.TimelineId).ToListAsync(); - entities = await Database.Timelines.Where(t => t.OwnerId == relatedUserId || timelineAsMemberIds.Contains(t.Id)).Include(t => t.Members).ToListAsync(); + entities = new List(); + + if ((relate.Type & TimelineUserRelationshipType.Own) != 0) + { + entities.AddRange(await Database.Timelines.Where(t => t.OwnerId == relate.UserId && t.Name != null).Include(t => t.Members).ToListAsync()); + } + + if ((relate.Type & TimelineUserRelationshipType.Join) != 0) + { + entities.AddRange(await Database.TimelineMembers.Where(m => m.UserId == relate.UserId).Include(m => m.Timeline).ThenInclude(t => t.Members).Select(m => m.Timeline).ToListAsync()); + } } var result = new List(); -- cgit v1.2.3