public async Task SetItem(JabberId jid, OtrSaveMode saveMode)
        {
            using (var context = this.CreateContext()) {
                var user = await GetUser(context);

                var savedItem = await
                                    (from item in context.UserItemPreferences
                                    where item.ArchivingUserId == UserId && item.Jid == jid.FullId
                                    select item).FirstOrDefaultAsync();
                if (savedItem == null)
                {
                    savedItem = new Model.DbUserItemPreference();
                    savedItem.ArchivingUserId = UserId;
                    savedItem.Jid             = jid.FullId;
                    context.UserItemPreferences.Add(savedItem);
                }

                if (saveMode.Expire != FSharpOption <long> .None)
                {
                    savedItem.Expire = FSharpHelper.FromFSharpS(saveMode.Expire);
                }
                if (saveMode.OtrMode != FSharpOption <OtrMode> .None)
                {
                    savedItem.OtrMode = FromFSharp(saveMode.OtrMode.Value);
                }
                if (saveMode.SaveMode != FSharpOption <SaveMode> .None)
                {
                    savedItem.SaveMode = FromFSharp(saveMode.SaveMode.Value);
                }

                await context.MySaveChanges();
            }
        }
        public async Task SetDefaultOtrSaveMode(FSharpOption <OtrSaveMode> value)
        {
            using (var context = this.CreateContext()) {
                var user = await GetUser(context);

                if (value == FSharpOption <OtrSaveMode> .None)
                {
                    user.Expire   = null;
                    user.OtrMode  = Model.OtrMode.NotSet;
                    user.SaveMode = Model.SaveMode.NotSet;
                }
                else
                {
                    var saveMode = value.Value;

                    if (saveMode.Expire != FSharpOption <long> .None)
                    {
                        user.Expire = FSharpHelper.FromFSharpS(saveMode.Expire);
                    }
                    if (saveMode.OtrMode != FSharpOption <OtrMode> .None)
                    {
                        user.OtrMode = FromFSharp(saveMode.OtrMode.Value);
                    }
                    if (saveMode.SaveMode != FSharpOption <SaveMode> .None)
                    {
                        user.SaveMode = FromFSharp(saveMode.SaveMode.Value);
                    }
                }

                await context.MySaveChanges();
            }
        }
        public async Task SetArchiving(FSharpOption <bool> value)
        {
            using (var context = this.CreateContext()) {
                var user = await GetUser(context);

                if (value != FSharpOption <bool> .None)
                {
                    user.AutomaticArchiving = FSharpHelper.FromFSharpS(value);
                }
                await context.MySaveChanges();
            }
        }
        public async Task <FSharpOption <StoredPreferenceInfo> > GetUserPreferences()
        {
            using (var context = this.CreateContext()) {
                var user = await GetUser(context);

                var isDefaultModeUnset =
                    user.OtrMode == Model.OtrMode.NotSet && user.SaveMode == Model.SaveMode.NotSet && user.Expire == null;

                var isAnyArchivingModeUnset =
                    user.LocalPreference == Model.ArchivingMode.NotSet ||
                    user.ManualPreference == Model.ArchivingMode.NotSet ||
                    user.AutoPreference == Model.ArchivingMode.NotSet;

                var isAllUnset =
                    isDefaultModeUnset &&
                    user.ItemPreferences.Count == 0 && isAnyArchivingModeUnset && user.AutomaticArchiving == null;
                if (isAllUnset)
                {
                    return(FSharpOption <StoredPreferenceInfo> .None);
                }

                var archivingModePrefs =
                    !isAnyArchivingModeUnset
                                        ? FSharpOption <AllMethodSettings> .Some(
                        new AllMethodSettings (
                            ToFSharp(user.AutoPreference),
                            ToFSharp(user.LocalPreference),
                            ToFSharp(user.ManualPreference)))
                                        : FSharpOption <AllMethodSettings> .None;

                var result = new StoredPreferenceInfo(
                    FSharpHelper.ToFSharpS(user.AutomaticArchiving),
                    new OtrSaveMode(
                        FSharpHelper.ToFSharpS <Int64> (user.Expire),
                        ToFSharp(user.OtrMode),
                        ToFSharp(user.SaveMode)),
                    ListModule.OfSeq <Tuple <JabberId, OtrSaveMode> >
                        (user.ItemPreferences.Select(item =>
                                                     Tuple.Create(
                                                         JabberId.Parse(item.Jid),
                                                         new OtrSaveMode(
                                                             FSharpHelper.ToFSharpS <Int64> (item.Expire),
                                                             ToFSharp(item.OtrMode),
                                                             ToFSharp(item.SaveMode))))),
                    archivingModePrefs
                    );
                return(FSharpOption <StoredPreferenceInfo> .Some(result));
            }
        }
Ejemplo n.º 5
0
        public void AutoPropertiesFromFSharpAreHandled()
        {
            const string source = @"
namespace FileHelpers

open System
open FileHelpers

[<CLIMutable>]
[<FixedLengthRecord>]
type SampleFSharpType = 
    { [<FieldFixedLength(8)>]
      [<FieldConverter(ConverterKind.Date, ""ddMMyyyy"")>]
      Field1: DateTime

      [<FieldFixedLength(3)>]
      [<FieldAlign(AlignMode.Left, ' ')>]
      [<FieldTrim(TrimMode.Both)>]
      Field2: string

      [<FieldFixedLength(3)>]
      [<FieldAlign(AlignMode.Right, '0')>]
      [<FieldTrim(TrimMode.Both)>]
      [<FieldOptional>]
      Field3: int }
";

            var assembly = FSharpHelper.Compile(source);
            var type     = assembly.GetType("FileHelpers.SampleFSharpType");

            var fileHelpersAssembly = typeof(EngineBase).Assembly;
            var dateConverterType   = fileHelpersAssembly.GetType("FileHelpers.Converters.DateTimeConverter");

            var engine = new FileHelperEngine(type);

            Check.That(engine.Options.FieldCount).IsEqualTo(3);

            Check.That(engine.Options.Fields[0].Converter.GetType()).IsEqualTo(dateConverterType);
            Check.That(engine.Options.Fields[2].IsOptional).IsEqualTo(true);
        }