Skip to content

Commit

Permalink
reduced .Where overhead, fixed a bug when deleting list videos and sm…
Browse files Browse the repository at this point in the history
…oothened transition to list mode
  • Loading branch information
MilchRatchet committed Nov 14, 2019
1 parent eddc08c commit fb9f33a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
21 changes: 14 additions & 7 deletions SubBox/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ValuesController(AppDbContext context)
[HttpGet("videos")]
public async Task<ActionResult<IEnumerable<Video>>> GetVideos()
{
var result = await context.Videos.Where(v => v.New).Where(v => v.List == 0).OrderByDescending(v => v.PublishedAt.Ticks).ToListAsync();
var result = await context.Videos.Where(v => v.New && v.List == 0).OrderByDescending(v => v.PublishedAt.Ticks).ToListAsync();

return result;
}
Expand All @@ -34,7 +34,7 @@ public async Task<ActionResult<IEnumerable<Video>>> GetVideos()
[HttpGet("videos/old")]
public async Task<ActionResult<IEnumerable<Video>>> GetOldVideos()
{
var result = await context.Videos.Where(v => !v.New).Where(v => v.List == 0).OrderByDescending(v => v.PublishedAt.Ticks).ToListAsync();
var result = await context.Videos.Where(v => !v.New && v.List == 0).OrderByDescending(v => v.PublishedAt.Ticks).ToListAsync();

return result;
}
Expand All @@ -52,7 +52,7 @@ public async Task<ActionResult<IEnumerable<Channel>>> GetChannels()
[HttpGet("lists")]
public async Task<ActionResult<IEnumerable<Video>>> GetLists()
{
var result = await context.Videos.Where(v => v.List != 0).Where(v => v.Index == 0).OrderBy(v => v.List).ToListAsync();
var result = await context.Videos.Where(v => v.List != 0 && v.Index == 0).OrderBy(v => v.List).ToListAsync();

return result;
}
Expand Down Expand Up @@ -142,7 +142,7 @@ public void NextEntry(int number)
{
context.Videos.Where(v => v.List == number).ToList().ForEach(v => v.Index -= 1);

Video video = context.Videos.Where(v => v.List == number).Where(v => v.Index == 1).FirstOrDefault();
Video video = context.Videos.Where(v => v.List == number && v.Index == 1).FirstOrDefault();

if (video == null)
{
Expand All @@ -158,7 +158,7 @@ public void NextEntry(int number)
[HttpPost("list/previous/{number}")]
public void PrevEntry(int number)
{
Video video = context.Videos.Where(v => v.List == number).Where(v => v.Index == -1).FirstOrDefault();
Video video = context.Videos.Where(v => v.List == number && v.Index == -1).FirstOrDefault();

if (video == null)
{
Expand Down Expand Up @@ -267,7 +267,14 @@ public void DeleteVideo(string id)
{
context.Videos.Remove(video);

context.Videos.Where(v => v.List == video.List).Where(v => v.Index > video.Index).ToList().ForEach(v => v.Index -= 1);
if (video.Index >= 0)
{
context.Videos.Where(v => v.List == video.List && v.Index > video.Index).ToList().ForEach(v => v.Index -= 1);
}
else
{
context.Videos.Where(v => v.List == video.List && v.Index < video.Index).ToList().ForEach(v => v.Index += 1);
}
}

context.SaveChanges();
Expand All @@ -293,7 +300,7 @@ public void DeleteChannel(string id)

context.Channels.Remove(channel);

IEnumerable<Video> videos = context.Videos.Where(v => v.List == 0).Where(v => v.ChannelId == id).ToList();
IEnumerable<Video> videos = context.Videos.Where(v => v.List == 0 && v.ChannelId == id).ToList();

foreach (Video v in videos)
{
Expand Down
2 changes: 1 addition & 1 deletion SubBox/SubBox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
22 changes: 15 additions & 7 deletions SubBox/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -889,9 +889,9 @@
},
async showUniqueList(number) {
if (number > 0) {
var result = await fetch("/api/values/list/all/" + number);
this.uniqueList = null;

this.uniqueList = await result.json();
var waiter = fetch("/api/values/list/all/" + number);

this.inputListModeNumber = number;

Expand All @@ -900,18 +900,24 @@
this.uniqueListMode = true;

this.videoListMode = false;

var result = await waiter;

this.uniqueList = await result.json();
}
},
async showOldVideos() {
var result = await fetch("api/values/videos/old");

this.oldVideos = await result.json();
var waiter = fetch("api/values/videos/old");

this.channelMode = false;

this.trashbinMode = true;

this.videoListMode = false;

var result = await waiter;

this.oldVideos = await result.json();
},
showSettings() {
this.channelMode = false;
Expand Down Expand Up @@ -998,7 +1004,7 @@

this.listUpdate();
} else {
fetch("/api/values/video/" + video.id, { method: "DELETE" });
var waiter = fetch("/api/values/video/" + video.id, { method: "DELETE" });

var index = this.videos.indexOf(video);

Expand All @@ -1020,11 +1026,13 @@
window.open("https://www.youtube.com/user/" + link, "_blank");
},
async showChannels() {
var waiter = fetch("/api/values/channels");

this.settingsMode = false;

this.channelMode = !this.channelMode;

var result = await fetch("/api/values/channels");
var result = await waiter;

this.channels = await result.json();

Expand Down

0 comments on commit fb9f33a

Please sign in to comment.