Skip to content

Commit 9e7305e

Browse files
committed
(#147) Web: initial controller implementation
1 parent f5d9427 commit 9e7305e

11 files changed

Lines changed: 82 additions & 54 deletions

File tree

Emulsion.ContentProxy/ContentStorage.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ let getOrCreateMessageRecord (context: EmulsionDbContext) (id: MessageContentIde
3232
| Some item -> return item
3333
}
3434

35-
let getById (context: EmulsionDbContext) (id: int64): Async<TelegramContent> = async {
35+
let getById (context: EmulsionDbContext) (id: int64): Async<TelegramContent option> = async {
3636
return! query {
3737
for content in context.TelegramContents do
3838
where (content.Id = id)
39-
} |> exactlyOneAsync
39+
} |> tryExactlyOneAsync
4040
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="Settings.fs" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\Emulsion.Database\Emulsion.Database.fsproj" />
14+
</ItemGroup>
15+
16+
</Project>

Emulsion.Tests/Telegram/LinkGeneratorTests.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ let private doDatabaseLinksTest (fileIds: string[]) message =
155155
let! content = DataStorage.transaction databaseSettings (fun context ->
156156
ContentStorage.getById context (Proxy.decodeHashId hostingSettings.HashIdSalt id)
157157
)
158+
let content = Option.get content
158159

159160
Assert.Equal(message.MessageId, content.MessageId)
160161
Assert.Equal(message.Chat.Username, Some content.ChatUserName)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace Emulsion.Web.Controllers
2+
3+
open System.Globalization
4+
open System.Threading.Tasks
5+
6+
open Microsoft.AspNetCore.Mvc
7+
open Microsoft.Extensions.Logging
8+
9+
open Emulsion.ContentProxy
10+
open Emulsion.Database
11+
open Emulsion.Settings
12+
13+
[<ApiController>]
14+
[<Route("content")>]
15+
type ContentController(logger: ILogger<ContentController>,
16+
configuration: HostingSettings,
17+
context: EmulsionDbContext) =
18+
inherit ControllerBase()
19+
20+
let decodeHashId hashId =
21+
try
22+
Some <| Proxy.decodeHashId configuration.HashIdSalt hashId
23+
with
24+
| ex ->
25+
logger.LogWarning(ex, "Error during hashId deserializing")
26+
None
27+
28+
let produceRedirect contentId: Async<IActionResult option> = async {
29+
let! content = ContentStorage.getById context contentId
30+
return
31+
content
32+
|> Option.map(fun c ->
33+
let url = $"https://t.me/{c.ChatUserName}/{c.MessageId.ToString(CultureInfo.InvariantCulture)}"
34+
RedirectResult url
35+
)
36+
}
37+
38+
39+
[<HttpGet>]
40+
member this.Get(hashId: string): Task<IActionResult> = task {
41+
match decodeHashId hashId with
42+
| None -> return this.BadRequest()
43+
| Some contentId ->
44+
match! produceRedirect contentId with
45+
| None -> return this.NotFound() :> IActionResult
46+
| Some redirect -> return redirect
47+
}

Emulsion.Web/Controllers/WeatherForecastController.fs

Lines changed: 0 additions & 35 deletions
This file was deleted.

Emulsion.Web/Emulsion.Web.fsproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<Compile Include="WeatherForecast.fs"/>
9-
<Compile Include="Controllers/WeatherForecastController.fs"/>
10-
<Compile Include="WebServer.fs"/>
8+
<Compile Include="Controllers\ContentController.fs" />
9+
<Compile Include="WebServer.fs" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\Emulsion.ContentProxy\Emulsion.ContentProxy.fsproj" />
14+
<ProjectReference Include="..\Emulsion.Settings\Emulsion.Settings.fsproj" />
1115
</ItemGroup>
1216
</Project>

Emulsion.Web/WeatherForecast.fs

Lines changed: 0 additions & 11 deletions
This file was deleted.

Emulsion.Web/WebServer.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ module Emulsion.Web.WebServer
33
open System
44
open System.Threading.Tasks
55

6-
open Emulsion.Web.Controllers
76
open Microsoft.AspNetCore.Builder
87
open Microsoft.Extensions.DependencyInjection
98

9+
open Emulsion.Web.Controllers
10+
1011
let run(baseUri: Uri): Task =
1112
// TODO: Pass baseUri
1213
let builder = WebApplication.CreateBuilder()
1314
builder.Services
1415
.AddControllers()
15-
.AddApplicationPart(typeof<WeatherForecastController>.Assembly)
16+
.AddApplicationPart(typeof<ContentController>.Assembly)
1617
|> ignore
1718

1819
let app = builder.Build()

Emulsion.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Emulsion.ContentProxy", "Em
4242
EndProject
4343
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Emulsion.Web", "Emulsion.Web\Emulsion.Web.fsproj", "{8EFD8F6C-43D3-4CE0-ADB8-63401E4A64FE}"
4444
EndProject
45+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Emulsion.Settings", "Emulsion.Settings\Emulsion.Settings.fsproj", "{88331E40-EF70-4AF1-99CA-8A849208CAB7}"
46+
EndProject
4547
Global
4648
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4749
Debug|Any CPU = Debug|Any CPU
@@ -71,6 +73,10 @@ Global
7173
{8EFD8F6C-43D3-4CE0-ADB8-63401E4A64FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
7274
{8EFD8F6C-43D3-4CE0-ADB8-63401E4A64FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
7375
{8EFD8F6C-43D3-4CE0-ADB8-63401E4A64FE}.Release|Any CPU.Build.0 = Release|Any CPU
76+
{88331E40-EF70-4AF1-99CA-8A849208CAB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
77+
{88331E40-EF70-4AF1-99CA-8A849208CAB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
78+
{88331E40-EF70-4AF1-99CA-8A849208CAB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
79+
{88331E40-EF70-4AF1-99CA-8A849208CAB7}.Release|Any CPU.Build.0 = Release|Any CPU
7480
EndGlobalSection
7581
GlobalSection(NestedProjects) = preSolution
7682
{7D1ADF47-BF1C-4007-BB9B-08C283044467} = {973131E1-E645-4A50-A0D2-1886A1A8F0C6}

0 commit comments

Comments
 (0)