public void Update(DatePreference obj)
 {
     using (var context = new EPDbContext())
     {
         context.Entry(obj).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
예제 #2
0
        public async Task <IActionResult> Create([Bind("Day,IsPreffered")] DatePreferenceViewModel vm)
        {
            var user = await _context.User.SingleOrDefaultAsync(m => m.Email == User.Identity.Name);

            if (user == null)
            {
                return(NotFound("User with given ID doesn't exist."));
            }

            var shift = await _context.Shift.SingleOrDefaultAsync(m => m.ShiftDate == vm.Day);

            if (shift == null)
            {
                shift = new Shift()
                {
                    IsShorterDay = false,
                    ShiftDate    = vm.Day
                };

                try
                {
                    _context.Add(shift);
                    await _context.SaveChangesAsync();
                }
                catch
                {
                    return(NotFound("Desired date preference doesn't have corresponding shift available."));
                }
            }

            var datePreference = new DatePreference()
            {
                IsPreffered = vm.IsPreffered,
                ShiftId     = shift.Id,
                UserId      = user.Id
            };

            if (ModelState.IsValid)
            {
                _context.Add(datePreference);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index), "Home"));
            }

            ViewData["UserId"] = new SelectList(_context.User, "Id", "Id", user.Id);
            return(View(vm));
        }
        protected override T OnBuildElement <T>(XElement element)
        {
            var category = new PreferenceCategory();

            category.FriendlyName = XElementHelper.ReadStringValue(element, "friendlyName");

            // Read the list of Preference tags
            var foundPreferences = XElementHelper.ReadEnumerable(element, "preference");

            // For each preference Tag, store the values into a new Preference object
            foreach (var foundPreference in foundPreferences)
            {
                // Ugly way of reading shared properties without duplicating code.
                Action <IPreference> readSharedProperties = preference =>
                {
                    preference.Name         = XElementHelper.ReadStringValue(foundPreference, "name");
                    preference.FriendlyName = XElementHelper.ReadStringValue(foundPreference, "friendlyName");
                    preference.Description  = XElementHelper.ReadStringValue(foundPreference, "description", false);

                    preference.HasDirectlyEditableValue = XElementHelper.ReadBoolValue(foundPreference,
                                                                                       "hasDirectlyEditableValue", false);

                    preference.useCurlyBraces = XElementHelper.ReadBoolValue(foundPreference,
                                                                             "useCurlyBraces", false);

                    preference.AllowMultipleSelections = XElementHelper.ReadBoolValue(foundPreference, "allowMultipleSelections", false);

                    category.Preferences.Add(preference);
                };

                // Determine preference type
                var isDate    = XElementHelper.ReadBoolValue(foundPreference, "isDate", false);
                var isNumeric = XElementHelper.ReadBoolValue(foundPreference, "isNumeric", false);
                var allowsMultipleSelections = XElementHelper.ReadBoolValue(foundPreference, "allowMultipleSelections", false);

                if (isDate)
                {
                    var preference = new DatePreference();

                    preference.DateFormat = XElementHelper.ReadStringValue(foundPreference, "dateFormat");
                    preference.MinValue   = XElementHelper.ReadDateValue(foundPreference, "minDateValue",
                                                                         preference.DateFormat, false);
                    preference.MaxValue = XElementHelper.ReadDateValue(foundPreference, "maxDateValue",
                                                                       preference.DateFormat, false);
                    preference.Value = XElementHelper.ReadDateValue(foundPreference, "value", preference.DateFormat,
                                                                    true);

                    // Read the list of entryOption tags
                    var foundEntries = XElementHelper.ReadEnumerable(foundPreference, "entryOption", false);

                    // For each tag, read the values into a new PreferenceEntry object
                    foreach (var entry in foundEntries)
                    {
                        preference.Entries.Add(BuildPreferenceEntry <IDatePreferenceEntry>(preference, entry));
                    }

                    readSharedProperties(preference);
                }
                else if (isNumeric)
                {
                    var preference = new NumericPreference();

                    preference.MinValue = XElementHelper.ReadDoubleValue(foundPreference, "minValue", false);
                    preference.MaxValue = XElementHelper.ReadDoubleValue(foundPreference, "maxValue", false);
                    preference.Value    = XElementHelper.ReadDoubleValue(foundPreference, "value", true);

                    // Read the list of entryOption tags
                    var foundEntries = XElementHelper.ReadEnumerable(foundPreference, "entryOption", false);

                    // For each tag, read the values into a new PreferenceEntry object
                    foreach (var entry in foundEntries)
                    {
                        preference.Entries.Add(BuildPreferenceEntry <INumericPreferenceEntry>(preference, entry));
                    }

                    readSharedProperties(preference);
                }
                else if (allowsMultipleSelections)
                {
                    var preference = new StringListPreference();

                    //preference.MinValue = XElementHelper.ReadStringValue(foundPreference, "minValue", false);
                    //preference.MaxValue = XElementHelper.ReadStringValue(foundPreference, "maxValue", false);
                    //preference.Value = XElementHelper.ReadStringValue(foundPreference, "value", false);

                    // Read the list of entryOption tags
                    var foundEntries = XElementHelper.ReadEnumerable(foundPreference, "entryOption", false);

                    // For each tag, read the values into a new PreferenceEntry object
                    foreach (var entry in foundEntries)
                    {
                        preference.Entries.Add(BuildPreferenceEntry <IStringListPreferenceEntry>(preference, entry));
                    }

                    readSharedProperties(preference);
                }
                else
                {
                    var preference = new StringPreference();

                    preference.MinValue = XElementHelper.ReadStringValue(foundPreference, "minValue", false);
                    preference.MaxValue = XElementHelper.ReadStringValue(foundPreference, "maxValue", false);
                    preference.Value    = XElementHelper.ReadStringValue(foundPreference, "value", false);

                    // Read the list of entryOption tags
                    var foundEntries = XElementHelper.ReadEnumerable(foundPreference, "entryOption", false);

                    // For each tag, read the values into a new PreferenceEntry object
                    foreach (var entry in foundEntries)
                    {
                        preference.Entries.Add(BuildPreferenceEntry <IStringPreferenceEntry>(preference, entry));
                    }

                    readSharedProperties(preference);
                }
            }

            return(category as T);
        }