public async Task AddAppointment(IOperation operation, AppointmentManageItemEntity entity)
        {
            // TODO: Validate appointment time
            var appointmentId = await appointmentStore.Add(operation, entity);

            await appointmentStore.AddHistoryRecord(operation, appointmentId, "Appointment was created");
        }
        public async Task UpdateAsClient(IOperation operation, AppointmentManageItemEntity entity)
        {
            await operation.ExecuteAsync(new
            {
                entity.Id,
                entity.StartTime,
                entity.CarWashServiceIds
            }, @"
                UPDATE
                SET [RequestedStartTime] = @StartTime
                FROM [appointment].[Appointment]
                WHERE [Id] = @Id;

                MERGE INTO [appointment].[AppointmentCarWashService] t
                USING (
                    SELECT
                        @Id    [AppointmentId],
                        [Id]   [CarWashServiceId]
                    FROM [company].[CarWashService]
                    WHERE [CarWashServiceId] IN @CarWashServiceIds
                ) s ON t.[AppointmentId] = s.[AppointmentId] AND t.[CarWashServiceId] = s.[CarWashServiceId]
                WHEN NOT MATCHED BY TARGET THEN INSERT (
                    [AppointmentId],
                    [CarWashServiceId]
                )
                VALUES (
                    s.[AppointmentId],
                    s.[CarWashServiceId]
                )
                WHEN NOT MATCHED BY SOURCE THEN DELETE;
            ");
        }
        public async Task UpdateAppointment(IOperation operation, UserRole role, AppointmentManageItemEntity entity)
        {
            if (!await appointmentStore.IsExist(operation, entity.Id))
            {
                throw new Exception(ExceptionMessage.AppointmentIsNotExist);
            }

            // TODO: Validate appointment time
            await appointmentStore.UpdateAsClient(operation, entity);

            await appointmentStore.AddHistoryRecord(operation, entity.Id, $"Appointment was updated by \"{role.GetUserRoleName()}\"");
        }
 public async Task UpdateAsCompany(IOperation operation, AppointmentManageItemEntity entity)
 {
     await operation.ExecuteAsync(new
     {
         entity.Id,
         entity.StartTime
     }, @"
         UPDATE [appointment].[Appointment]
         SET [ApprovedStartTime] = @StartTime
         WHERE [Id] = @Id;
     ");
 }
        public async Task <int> Add(IOperation operation, AppointmentManageItemEntity entity)
        {
            return(await operation.QuerySingleAsync <int>(new
            {
                entity.CarId,
                entity.CarWashId,
                entity.StartTime,
                entity.CarWashServiceIds
            }, @"
                DECLARE @IdTable TABLE ([Id] INT);

                INSERT INTO [appointment].[Appointment] (
                    [CarId],
                    [CarWashId],
                    [RequestedStartTime],
                    [StatusId]
                )
                OUTPUT INSERTED.[Id] INTO @IdTable
                VALUES (
                    @CarId,
                    @CarWashId,
                    @StartTime,
                    1 -- Opened
                );

                DECLARE @AppointmentId INT;
                SELECT @AppointmentId = [Id] FROM @IdTable;

                INSERT INTO [appointment].[AppointmentCarWashService] (
                    [AppointmentId],
                    [CarWashServiceId]
                )
                SELECT
                    @AppointmentId,
                    [Id]
                FROM [company].[CarWashService]
                WHERE [Id] IN @CarWashServiceIds;

                SELECT @AppointmentId;
            "));
        }