public async Task <IActionResult> OnPostAsync(int?userId, int?salonId, bool?isSelected)
        {
            if (userId == null || salonId == null || isSelected == null)
            {
                return(RedirectToPage("./Index"));
            }
            var user = await _userManager.FindByIdAsync(userId.Value.ToString());

            if (user == null)
            {
                return(NotFound("Nerastas toks vartotojas."));
            }

            if (!await _userManager.IsInRoleAsync(user, "Staff"))
            {
                return(NotFound());
            }

            if (isSelected.Value)
            {
                var staffSalonEntry = await _context.StaffSalons
                                      .Where(r => r.StaffId == userId.Value && r.SalonId == salonId.Value).FirstOrDefaultAsync();

                if (staffSalonEntry == null)
                {
                    return(NotFound());
                }

                _context.StaffSalons.Remove(staffSalonEntry);
            }
            else if (!isSelected.Value)
            {
                StaffSalon         = new StaffSalon();
                StaffSalon.SalonId = salonId.Value;
                StaffSalon.StaffId = userId.Value;
                _context.Add(StaffSalon);
            }
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                return(NotFound("Nepavyko atnaujinti."));
            }
            return(RedirectToPage("./AssignStaffToSalonsEdit", new { id = userId.Value }));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPostCreateNotificationAsync()
        {
            int    userId = PostDataValues.UserId;
            string link   = PostDataValues.Link;
            {
                MemoryStream stream = new MemoryStream();
                Request.Body.CopyTo(stream);
                stream.Position = 0;
                using (StreamReader reader = new StreamReader(stream))
                {
                    string requestBody = reader.ReadToEnd();
                    if (requestBody.Length > 0)
                    {
                        var settings = new JsonSerializerSettings
                        {
                            NullValueHandling     = NullValueHandling.Ignore,
                            MissingMemberHandling = MissingMemberHandling.Ignore,
                        };
                        try
                        {
                            var obj = JsonConvert.DeserializeObject <PostData>(requestBody, settings);
                            if (obj != null && !Tools.Comparer.IsDefaultValue(obj.UserId) &&
                                !Tools.Comparer.IsDefaultValue(obj.Link))
                            {
                                userId = obj.UserId;
                                link   = obj.Link;

                                var notification = new Notification(userId, link, (NotiflicationType)2);
                                _context.Add(notification);
                                await _context.SaveChangesAsync();
                            }
                            else
                            {
                                throw new Exception("Tuščios vertės buvo perduotos");
                            }
                        }
                        catch (Exception e) { return(BadRequest(e)); }
                    }
                }
                return(new OkResult());
            }
        }