Skip to content

Commit

Permalink
Adding Workshops
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzoukr committed Apr 8, 2019
1 parent 0e032da commit 40df600
Show file tree
Hide file tree
Showing 26 changed files with 422 additions and 57 deletions.
17 changes: 17 additions & 0 deletions database/003-AddWorkshops.sql
@@ -0,0 +1,17 @@
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Workshops](
[Id] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](max) NOT NULL,
[Description] [nvarchar](max) NOT NULL,
[StartDate] [datetimeoffset](7) NOT NULL,
[EndDate] [datetimeoffset](7) NOT NULL,
[Created] [datetimeoffset](7) NOT NULL,
CONSTRAINT [PK_Workshops] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
2 changes: 1 addition & 1 deletion database/yobo.schema

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion global.json
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "2.2.105"
"version": "2.2.100"
}
}
25 changes: 25 additions & 0 deletions src/Yobo.API/Admin/Functions.fs
Expand Up @@ -34,6 +34,18 @@ module ArgsBuilder =
) Validation.validateAddLesson
>> Result.mapError ServerError.ValidationError

let buildAddWorkshop =
ArgsBuilder.build (fun (x:AddWorkshop) ->
({
Id = Guid.NewGuid()
StartDate = x.Start
EndDate = x.End
Name = x.Name
Description = x.Description
} : Workshops.CmdArgs.Create)
) Validation.validateAddWorkshop
>> Result.mapError ServerError.ValidationError


let addCredits cmdHandler (acc:AddCredits) =
result {
Expand All @@ -49,8 +61,21 @@ let addLessons cmdHandler (acc:AddLesson list) =
return ()
}

let addWorkshops cmdHandler (acc:AddWorkshop list) =
result {
let! args = acc |> Result.traverse ArgsBuilder.buildAddWorkshop
let! _ = args |> Result.traverse (Workshops.Command.Create >> CoreCommand.Workshops >> cmdHandler)
return ()
}

let cancelLesson cmdHandler (i:Guid) =
result {
let! _ = ({ Id = i } : Lessons.CmdArgs.Cancel) |> Lessons.Command.Cancel |> CoreCommand.Lessons |> cmdHandler
return ()
}

let deleteWorkshop cmdHandler (i:Guid) =
result {
let! _ = ({ Id = i } : Workshops.CmdArgs.Delete) |> Workshops.Command.Delete |> CoreCommand.Workshops |> cmdHandler
return ()
}
3 changes: 3 additions & 0 deletions src/Yobo.API/CompositionRoot.Communication.fs
Expand Up @@ -77,8 +77,11 @@ module Admin =
GetAllUsers = fun x -> x |> Security.onlyForAdmin <!> snd >>= Services.Users.queries.GetAll |> toAsync
AddCredits = fun x -> x |> Security.onlyForAdmin >>= Security.handleForUser addCredits |> toAsync
GetLessonsForDateRange = fun x -> x |> Security.onlyForAdmin <!> snd >>= Services.Lessons.queries.GetAllForDateRange |> toAsync
GetWorkshopsForDateRange = fun x -> x |> Security.onlyForAdmin <!> snd >>= Services.Workshops.queries.GetAllForDateRange |> toAsync
AddLessons = fun x -> x |> Security.onlyForAdmin >>= Security.handleForUser addLessons |> toAsync
AddWorkshops = fun x -> x |> Security.onlyForAdmin >>= Security.handleForUser addWorkshops |> toAsync
CancelLesson = fun x -> x |> Security.onlyForAdmin >>= Security.handleForUser cancelLesson |> toAsync
DeleteWorkshop = fun x -> x |> Security.onlyForAdmin >>= Security.handleForUser deleteWorkshop |> toAsync
}

module Calendar =
Expand Down
8 changes: 8 additions & 0 deletions src/Yobo.API/CompositionRoot.Services.fs
Expand Up @@ -48,10 +48,17 @@ module Lessons =
|> Lessons.ReadQueries.createDefault
|> Lessons.ReadQueries.withError dbErrorToServerError

module Workshops =
let queries =
Configuration.ReadDb.connectionString
|> Workshops.ReadQueries.createDefault
|> Workshops.ReadQueries.withError dbErrorToServerError

// event handlers
module EventHandler =
open Yobo.Core.Users.EventSerializer
open Yobo.Core.Lessons.EventSerializer
open Yobo.Core.Workshops.EventSerializer

let private dbHandleFn = DbEventHandler.getHandler Configuration.ReadDb.connectionString
let private emailHandleFn = EmailEventHandler.getHandler Users.queries emailService emailSettings
Expand All @@ -61,6 +68,7 @@ module EventHandler =

eventStore.EventAppended.Add(function
| LessonsEvent evn -> evn |> CoreEvent.Lessons |> handle
| WorkshopsEvent evn -> evn |> CoreEvent.Workshops |> handle
| UsersEvent cryptoProvider evn -> evn |> CoreEvent.Users |> handle
| _ -> ()
)
Expand Down
44 changes: 28 additions & 16 deletions src/Yobo.Client/Admin/Lessons/Domain.fs
Expand Up @@ -4,41 +4,53 @@ open System
open Yobo.Shared.Communication
open Yobo.Shared.Domain

type State = {
Lessons : Lesson list
WeekOffset : int
SelectedDates : DateTimeOffset list
type AddLessonForm = {
StartTime : string
EndTime : string
Name : string
Description : string
FormOpened : bool
}
with
static member Init = {
Lessons = []
WeekOffset = 0
SelectedDates = []
StartTime = ""
EndTime = ""
Name = ""
Description = ""
}

type State = {
Lessons : Lesson list
Workshops : Workshop list
WeekOffset : int
SelectedDates : DateTimeOffset list
FormOpened : bool
AddLessonForm : AddLessonForm
}
with
static member Init = {
Lessons = []
Workshops = []
WeekOffset = 0
SelectedDates = []
FormOpened = false
AddLessonForm = AddLessonForm.Init
}

type Msg =
| Init
| LoadLessons
| LessonsLoaded of ServerResult<Lesson list>
| LoadWorkshops
| WorkshopsLoaded of ServerResult<Workshop list>
| WeekOffsetChanged of int
| DateSelected of DateTimeOffset
| DateUnselected of DateTimeOffset
| StartChanged of string
| FormOpened of bool
| EndChanged of string
| NameChanged of string
| DescriptionChanged of string
| SubmitLessonsForm
| LessonsFormSubmitted of ServerResult<unit>
| AddLessonFormOpened of bool
| AddLessonFormChanged of AddLessonForm
| SubmitAddLessonForm
| SubmitAddWorkshopForm
| AddLessonFormSubmitted of ServerResult<unit>
| CancelLesson of Guid
| LessonCancelled of ServerResult<unit>
| DeleteWorkshop of Guid
| LessonCancelled of ServerResult<unit>
| WorkshopDeleted of ServerResult<unit>
78 changes: 57 additions & 21 deletions src/Yobo.Client/Admin/Lessons/State.fs
Expand Up @@ -29,68 +29,104 @@ let getValidLessonsToAdd (state:State) =
|> List.map (fun x ->
let st,en =
x
|> tryGetFromTo state.StartTime state.EndTime
|> tryGetFromTo state.AddLessonForm.StartTime state.AddLessonForm.EndTime
|> Option.defaultValue (DateTimeOffset.MinValue, DateTimeOffset.MinValue)
({
Start = st
End = en
Name = state.Name
Description = state.Description
Name = state.AddLessonForm.Name
Description = state.AddLessonForm.Description
} : Yobo.Shared.Admin.Domain.AddLesson)
)
|> List.filter (fun x -> x.Start <> DateTimeOffset.MinValue)
|> List.map Yobo.Shared.Admin.Validation.validateAddLesson
|> Result.partition
|> fst

let getValidWorkshopsToAdd (state:State) =
state.SelectedDates
|> List.map (fun x ->
let st,en =
x
|> tryGetFromTo state.AddLessonForm.StartTime state.AddLessonForm.EndTime
|> Option.defaultValue (DateTimeOffset.MinValue, DateTimeOffset.MinValue)
({
Start = st
End = en
Name = state.AddLessonForm.Name
Description = state.AddLessonForm.Description
} : Yobo.Shared.Admin.Domain.AddWorkshop)
)
|> List.filter (fun x -> x.Start <> DateTimeOffset.MinValue)
|> List.map Yobo.Shared.Admin.Validation.validateAddWorkshop
|> Result.partition
|> fst

let update (msg : Msg) (state : State) : State * Cmd<Msg> =
match msg with
| Init -> state, LoadLessons |> Cmd.ofMsg
| Init -> state, [ LoadLessons; LoadWorkshops] |> List.map Cmd.ofMsg |> Cmd.batch
| LoadLessons ->
state,
state.WeekOffset
|> DateRange.getDateRangeForWeekOffset
|> SecuredParam.create
|> Cmd.ofAsyncResult adminAPI.GetLessonsForDateRange LessonsLoaded
| LoadWorkshops ->
state,
state.WeekOffset
|> DateRange.getDateRangeForWeekOffset
|> SecuredParam.create
|> Cmd.ofAsyncResult adminAPI.GetWorkshopsForDateRange WorkshopsLoaded
| LessonsLoaded res ->
match res with
| Ok less ->
{ state with Lessons = less}, Cmd.none
| Error _ -> state, Cmd.none
| WorkshopsLoaded res ->
match res with
| Ok v ->
{ state with Workshops = v}, Cmd.none
| Error _ -> state, Cmd.none
| WeekOffsetChanged o ->
{ state with WeekOffset = o }, LoadLessons |> Cmd.ofMsg
| DateSelected d ->
{ state with SelectedDates = d :: state.SelectedDates }, Cmd.none
| DateUnselected d ->
let newDates = state.SelectedDates |> List.filter (fun x -> x <> d )
let newCmd = if newDates.Length > 0 then Cmd.none else (FormOpened(false) |> Cmd.ofMsg)
let newCmd = if newDates.Length > 0 then Cmd.none else (AddLessonFormOpened(false) |> Cmd.ofMsg)
{ state with SelectedDates = newDates }, newCmd
| StartChanged s ->
{ state with StartTime = s }, Cmd.none
| EndChanged s ->
{ state with EndTime = s }, Cmd.none
| NameChanged n ->
{ state with Name = n }, Cmd.none
| DescriptionChanged n ->
{ state with Description = n }, Cmd.none
| FormOpened o ->
| AddLessonFormChanged f ->
{ state with AddLessonForm = f }, Cmd.none
| AddLessonFormOpened o ->
{ state with FormOpened = o }, Cmd.none
| SubmitLessonsForm ->
| SubmitAddLessonForm ->
state,
(state
|> getValidLessonsToAdd
|> SecuredParam.create
|> Cmd.ofAsyncResult adminAPI.AddLessons (LessonsFormSubmitted))
| LessonsFormSubmitted res ->
|> Cmd.ofAsyncResult adminAPI.AddLessons (AddLessonFormSubmitted))
| SubmitAddWorkshopForm ->
state,
(state
|> getValidWorkshopsToAdd
|> SecuredParam.create
|> Cmd.ofAsyncResult adminAPI.AddWorkshops (AddLessonFormSubmitted))
| AddLessonFormSubmitted res ->
match res with
| Ok _ -> { State.Init with WeekOffset = state.WeekOffset },
[
SharedView.successToast "Lekce úspěšně přidány."
LoadLessons |> Cmd.ofMsg ]
|> Cmd.batch
[
SharedView.successToast "Lekce úspěšně přidány."
LoadLessons |> Cmd.ofMsg
LoadWorkshops |> Cmd.ofMsg
]
|> Cmd.batch
| Error e -> state, (e |> SharedView.serverErrorToToast)
| CancelLesson id ->
state, (id |> SecuredParam.create |> Cmd.ofAsyncResult adminAPI.CancelLesson LessonCancelled)
| DeleteWorkshop id ->
state, (id |> SecuredParam.create |> Cmd.ofAsyncResult adminAPI.DeleteWorkshop WorkshopDeleted)
| LessonCancelled res ->
state, [ (res |> SharedView.resultToToast "Lekce byla úspěšně zrušena"); LoadLessons |> Cmd.ofMsg ] |> Cmd.batch
| WorkshopDeleted res ->
state, [ (res |> SharedView.resultToToast "Workshop byla úspěšně smazán"); LoadWorkshops |> Cmd.ofMsg ] |> Cmd.batch

0 comments on commit 40df600

Please sign in to comment.