Beispiel #1
0
        // GET: VoiceNotes/Create
        public IActionResult Create()
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
            return(View());
        }
Beispiel #2
0
        // GET: SickWardenRelations/Delete/5
        public async Task <IActionResult> Delete(long?id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

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

            SickWarden sickWarden = await _context.SickWardenRelations
                                    .FirstOrDefaultAsync(m => m.Id == id);

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

            await _context.Entry(sickWarden).Reference(p => p.Sick).LoadAsync();

            await _context.Entry(sickWarden).Reference(p => p.Warden).LoadAsync();

            SickWardenDeleteViewModel viewModel = new SickWardenDeleteViewModel
            {
                Id       = sickWarden.Id,
                FullName = sickWarden.Warden.Equals(appUser) ? sickWarden.Sick.FullName : sickWarden.Warden.FullName
            };

            return(View(viewModel));
        }
Beispiel #3
0
        // GET: VoiceNotes/Delete/5
        public async Task <IActionResult> Delete(long?id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

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

            VoiceNote voiceNote = await _context.VoiceNotes
                                  .FirstOrDefaultAsync(m => m.Id == id);

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

            await _context.Entry(voiceNote).Reference(p => p.Owner).LoadAsync();

            if (!voiceNote.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(voiceNote.Owner) && p.IsAccepted.Equals(true)))
            {
                return(NotFound());
            }

            VoiceNoteIndexViewModel voiceNoteIndexViewModel = new VoiceNoteIndexViewModel
            {
                Id      = voiceNote.Id,
                Name    = voiceNote.Name,
                Comment = voiceNote.Comment,
                Owner   = voiceNote.Owner.FullName
            };

            return(View(voiceNoteIndexViewModel));
        }
Beispiel #4
0
        // GET: SickWardenRelations/Accept/5
        public async Task <IActionResult> Accept(long?id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

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

            SickWarden sickWarden = await _context.SickWardenRelations.FindAsync(id);

            if (sickWarden == null)
            {
                return(NotFound());
            }
            await _context.Entry(sickWarden).Reference(p => p.Sick).LoadAsync();

            await _context.Entry(sickWarden).Reference(p => p.Warden).LoadAsync();

            if (sickWarden.Sick.Equals(appUser) && sickWarden.IsProposedByWarden.GetValueOrDefault())
            {
                sickWarden.IsAccepted = true;
                _context.SickWardenRelations.Update(sickWarden);
                await _context.SaveChangesAsync();
            }
            else if (sickWarden.Warden.Equals(appUser) && !sickWarden.IsProposedByWarden.GetValueOrDefault())
            {
                sickWarden.IsAccepted = true;
                _context.SickWardenRelations.Update(sickWarden);
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction(nameof(Index)));
        }
Beispiel #5
0
        // GET: Devices
        public async Task <IActionResult> Index()
        {
            AppUser        appUser  = AuthExtender.GetLoggedInUser(this, _context);
            List <AppUser> allBound = _context.SickWardenRelations.Where(p => p.Warden.Equals(appUser) && p.IsAccepted.Equals(true)).Select(p => p.Sick).ToList();

            allBound.Add(appUser);

            List <Device> devices = await _context.Devices.Include(p => p.Owner).Where(p => allBound.Contains(p.Owner)).ToListAsync();

            List <DeviceIndexViewModel> deviceIndexViewModels = new List <DeviceIndexViewModel>();

            foreach (Device device in devices)
            {
                deviceIndexViewModels.Add(new DeviceIndexViewModel
                {
                    Id                  = device.Id,
                    Name                = device.Name,
                    Comment             = device.Comment,
                    Owner               = device.Owner.FullName,
                    IsServiceObligatory = device.IsServiceObligatory.GetValueOrDefault(),
                    LastPlaceLeft       = device.LastPlaceLeft,
                    LastServiceDate     = device.LastServiceDate,
                    LastUsedDate        = device.LastUsedDate
                });
            }

            return(View(deviceIndexViewModels));
        }
Beispiel #6
0
        public async Task <IActionResult> DeleteConfirmed(long id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            VoiceNote voiceNote = await _context.VoiceNotes.FindAsync(id);

            await _context.Entry(voiceNote).Reference(p => p.Owner).LoadAsync();

            if (!voiceNote.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(voiceNote.Owner) && p.IsAccepted.Equals(true)))
            {
                return(NotFound());
            }

            VoiceNoteIndexViewModel voiceNoteIndexViewModel = new VoiceNoteIndexViewModel
            {
                Id      = voiceNote.Id,
                Name    = voiceNote.Name,
                Comment = voiceNote.Comment,
                Owner   = voiceNote.Owner.FullName
            };

            _context.VoiceNotes.Remove(voiceNote);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Beispiel #7
0
        // GET: Materials
        public async Task <IActionResult> Index()
        {
            AppUser        appUser  = AuthExtender.GetLoggedInUser(this, _context);
            List <AppUser> allBound = _context.SickWardenRelations.Where(p => p.Warden.Equals(appUser) && p.IsAccepted.Equals(true)).Select(p => p.Sick).ToList();

            allBound.Add(appUser);

            List <Material> materials = await _context.Materials.Where(p => allBound.Contains(p.Owner)).ToListAsync();

            List <MaterialIndexViewModel> materialIndexViewModels = new List <MaterialIndexViewModel>();

            foreach (Material material in materials)
            {
                materialIndexViewModels.Add(new MaterialIndexViewModel
                {
                    Id            = material.Id,
                    Name          = material.Name,
                    Comment       = material.Comment,
                    Owner         = material.Owner.FullName,
                    LastPlaceLeft = material.LastPlaceLeft,
                    LastUsedDate  = material.LastUsedDate,
                    IsMedicine    = material.IsMedicine.GetValueOrDefault(),
                    Amount        = material.Amount,
                    MedicineDose  = material.MedicineDose,
                    Type          = material.Type
                });
            }

            return(View(materialIndexViewModels));
        }
Beispiel #8
0
        public async Task <IActionResult> Create(DeviceCreateViewModel deviceViewModel)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            /*if (true)
             * {
             *  ModelState.AddModelError("OwnerId", "Hack attempt!");
             *  return View(deviceViewModel);
             * }*/
            if (ModelState.IsValid)
            {
                AppUser newOwner = null;
                if (appUser.Id.Equals(deviceViewModel.OwnerId))
                {
                    newOwner = appUser;
                }
                else
                {
                    AppUser probablyNewOwner = _context.Users.FirstOrDefault(p => p.Id.Equals(deviceViewModel.OwnerId));
                    if (probablyNewOwner == null)
                    {
                        ModelState.AddModelError("OwnerId", "Hack attempt!");
                        ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                        return(View(deviceViewModel));
                    }

                    if (_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(probablyNewOwner) && p.IsAccepted.Equals(true)))
                    {
                        newOwner = probablyNewOwner;
                    }
                    else
                    {
                        ModelState.AddModelError("OwnerId", "Hack attempt!");
                        ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                        return(View(deviceViewModel));
                    }
                }

                Device device = new Device
                {
                    Id                  = deviceViewModel.Id,
                    Name                = deviceViewModel.Name,
                    Comment             = deviceViewModel.Comment,
                    Owner               = newOwner,
                    IsServiceObligatory = deviceViewModel.IsServiceObligatory,
                    LastPlaceLeft       = deviceViewModel.LastPlaceLeft,
                    LastServiceDate     = deviceViewModel.LastServiceDate,
                    LastUsedDate        = deviceViewModel.LastUsedDate
                };

                _context.Add(device);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
            return(View(deviceViewModel));
        }
Beispiel #9
0
        public async Task <IActionResult> Create(MaterialCreateViewModel materialViewModel)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            if (ModelState.IsValid)
            {
                AppUser newOwner = null;
                if (appUser.Id.Equals(materialViewModel.OwnerId))
                {
                    newOwner = appUser;
                }
                else
                {
                    AppUser probablyNewOwner = _context.Users.FirstOrDefault(p => p.Id.Equals(materialViewModel.OwnerId));
                    if (probablyNewOwner == null)
                    {
                        ModelState.AddModelError("OwnerId", "Hack attempt!");
                        ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                        return(View(materialViewModel));
                    }

                    if (_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(probablyNewOwner) && p.IsAccepted.Equals(true)))
                    {
                        newOwner = probablyNewOwner;
                    }
                    else
                    {
                        ModelState.AddModelError("OwnerId", "Hack attempt!");
                        ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                        return(View(materialViewModel));
                    }
                }

                Material material = new Material
                {
                    Id            = materialViewModel.Id,
                    Name          = materialViewModel.Name,
                    Comment       = materialViewModel.Comment,
                    Owner         = newOwner,
                    LastPlaceLeft = materialViewModel.LastPlaceLeft,
                    LastUsedDate  = materialViewModel.LastUsedDate,
                    IsMedicine    = materialViewModel.IsMedicine,
                    Amount        = materialViewModel.Amount,
                    MedicineDose  = materialViewModel.MedicineDose,
                    Type          = materialViewModel.Type
                };

                _context.Add(material);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
            return(View(materialViewModel));
        }
Beispiel #10
0
        public async Task <IActionResult> Create(SickWardenAddViewModel sickWardenAddViewModel)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            if (ModelState.IsValid)
            {
                Boolean isSick      = sickWardenAddViewModel.TypeId.Equals("sick");
                AppUser anotherUser = _context.Users.FirstOrDefault(p => p.Id.Equals(sickWardenAddViewModel.UserId));
                if (anotherUser == null)
                {
                    ModelState.AddModelError("UserId", "Hack attempt!");
                    ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                    ViewBag.TypeList = MakeTypeList();
                    return(View(sickWardenAddViewModel));
                }
                if (isSick)
                {
                    if (_context.SickWardenRelations.Any(p => p.Sick.Equals(appUser) && p.Warden.Equals(anotherUser)))
                    {
                        ModelState.AddModelError("UserId", "Hack attempt!");
                        ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                        ViewBag.TypeList = MakeTypeList();
                        return(View(sickWardenAddViewModel));
                    }
                }
                else
                {
                    if (_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(anotherUser)))
                    {
                        ModelState.AddModelError("UserId", "Hack attempt!");
                        ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                        ViewBag.TypeList = MakeTypeList();
                        return(View(sickWardenAddViewModel));
                    }
                }

                SickWarden sickWarden = new SickWarden
                {
                    Sick               = isSick ? appUser : anotherUser,
                    Warden             = isSick ? anotherUser : appUser,
                    IsAccepted         = false,
                    IsProposedByWarden = !isSick
                };

                _context.Add(sickWarden);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
            ViewBag.TypeList = MakeTypeList();
            return(View(sickWardenAddViewModel));
        }
Beispiel #11
0
        // GET: VoiceNotes/Details/5
        public async Task <IActionResult> Details(long?id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

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

            VoiceNote voiceNote = await _context.VoiceNotes
                                  .FirstOrDefaultAsync(m => m.Id == id);

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

            await _context.Entry(voiceNote).Reference(p => p.Owner).LoadAsync();

            if (!voiceNote.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(voiceNote.Owner) && p.IsAccepted.Equals(true)))
            {
                return(NotFound());
            }

            String content  = null;
            String fileName = voiceNote.FileName;

            if (fileName != null)
            {
                String type   = GetTagType(fileName);
                String base64 = await GetRecordingBase64(fileName);

                if (base64 != null)
                {
                    content = "data:" + type + ";base64," + base64;
                }
            }

            VoiceNoteIndexViewModel voiceNoteIndexViewModel = new VoiceNoteIndexViewModel
            {
                Id                 = voiceNote.Id,
                Name               = voiceNote.Name,
                Comment            = voiceNote.Comment,
                Owner              = voiceNote.Owner.FullName,
                RecordingRawBase64 = content
            };

            return(View(voiceNoteIndexViewModel));
        }
Beispiel #12
0
        public async Task <IActionResult> DeleteConfirmed(long id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            Material material = await _context.Materials.FindAsync(id);

            await _context.Entry(material).Reference(p => p.Owner).LoadAsync();

            if (!material.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(material.Owner) && p.IsAccepted.Equals(true)))
            {
                return(NotFound());
            }

            _context.Materials.Remove(material);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Beispiel #13
0
        public async Task <IActionResult> DeleteConfirmed(long id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            SickWarden sickWarden = await _context.SickWardenRelations.FindAsync(id);

            await _context.Entry(sickWarden).Reference(p => p.Sick).LoadAsync();

            await _context.Entry(sickWarden).Reference(p => p.Warden).LoadAsync();

            if (sickWarden.Warden.Equals(appUser) || sickWarden.Sick.Equals(appUser))
            {
                _context.SickWardenRelations.Remove(sickWarden);
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction(nameof(Index)));
        }
Beispiel #14
0
        public async Task <IActionResult> Download(long?id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

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

            VoiceNote voiceNote = await _context.VoiceNotes
                                  .FirstOrDefaultAsync(m => m.Id == id);

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

            await _context.Entry(voiceNote).Reference(p => p.Owner).LoadAsync();

            if (!voiceNote.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(voiceNote.Owner) && p.IsAccepted.Equals(true)))
            {
                return(NotFound());
            }

            var path = Path.Combine(
                Directory.GetCurrentDirectory(),
                "wwwroot\\UserRecordings", voiceNote.FileName);

            if (!System.IO.File.Exists(path))
            {
                return(NotFound());
            }
            var memory = new MemoryStream();

            using (var stream = new FileStream(path, FileMode.Open))
            {
                await stream.CopyToAsync(memory);
            }
            memory.Position = 0;
            return(File(memory, GetContentType(path), voiceNote.Name + Path.GetExtension(path).ToLowerInvariant()));
        }
Beispiel #15
0
        // GET: Materials/Edit/5
        public async Task <IActionResult> Edit(long?id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

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

            var material = await _context.Materials.FindAsync(id);

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

            await _context.Entry(material).Reference(p => p.Owner).LoadAsync();

            if (!material.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(material.Owner) && p.IsAccepted.Equals(true)))
            {
                return(NotFound());
            }

            ViewBag.UserList = MakeAllowedUsersList(appUser, material.Owner.Id);

            MaterialCreateViewModel materialViewModel = new MaterialCreateViewModel
            {
                Id            = material.Id,
                Name          = material.Name,
                Comment       = material.Comment,
                OwnerId       = material.Owner.Id,
                LastPlaceLeft = material.LastPlaceLeft,
                LastUsedDate  = material.LastUsedDate,
                IsMedicine    = material.IsMedicine.GetValueOrDefault(),
                Amount        = material.Amount,
                MedicineDose  = material.MedicineDose,
                Type          = material.Type
            };

            return(View(materialViewModel));
        }
Beispiel #16
0
        // GET: SickWardenRelations
        public async Task <IActionResult> Index()
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            List <SickWarden> wardenRawList = await _context.SickWardenRelations.Include(p => p.Warden).Where(p => p.Sick.Equals(appUser)).ToListAsync();

            List <SickWarden> sickRawList = await _context.SickWardenRelations.Include(p => p.Sick).Where(p => p.Warden.Equals(appUser)).ToListAsync();

            List <SickWardenIndexWardenElementViewModel> wardenList = new List <SickWardenIndexWardenElementViewModel>();
            List <SickWardenIndexSickElementViewModel>   sickList   = new List <SickWardenIndexSickElementViewModel>();

            foreach (SickWarden warden in wardenRawList)
            {
                wardenList.Add(new SickWardenIndexWardenElementViewModel
                {
                    Id         = warden.Id,
                    FullName   = warden.Warden.FullName,
                    IsAccepted = warden.IsAccepted.GetValueOrDefault(),
                    CanAccept  = warden.IsProposedByWarden.GetValueOrDefault(),
                });
            }
            foreach (SickWarden sick in sickRawList)
            {
                sickList.Add(new SickWardenIndexSickElementViewModel
                {
                    Id         = sick.Id,
                    FullName   = sick.Sick.FullName,
                    IsAccepted = sick.IsAccepted.GetValueOrDefault(),
                    CanAccept  = !sick.IsProposedByWarden.GetValueOrDefault()
                });
            }

            SickWardenIndexViewModel sickWardenIndexViewModel = new SickWardenIndexViewModel
            {
                WardenList = wardenList,
                SickList   = sickList
            };

            return(View(sickWardenIndexViewModel));
        }
Beispiel #17
0
        // GET: Materials/Details/5
        public async Task <IActionResult> Details(long?id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

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

            Material material = await _context.Materials
                                .FirstOrDefaultAsync(m => m.Id == id);

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

            await _context.Entry(material).Reference(p => p.Owner).LoadAsync();

            if (!material.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(material.Owner) && p.IsAccepted.Equals(true)))
            {
                return(NotFound());
            }

            MaterialIndexViewModel materialViewModel = new MaterialIndexViewModel
            {
                Id            = material.Id,
                Name          = material.Name,
                Comment       = material.Comment,
                Owner         = material.Owner.FullName,
                LastPlaceLeft = material.LastPlaceLeft,
                LastUsedDate  = material.LastUsedDate,
                IsMedicine    = material.IsMedicine.GetValueOrDefault(),
                Amount        = material.Amount,
                MedicineDose  = material.MedicineDose,
                Type          = material.Type
            };

            return(View(materialViewModel));
        }
Beispiel #18
0
        // GET: Devices/Edit/5
        public async Task <IActionResult> Edit(long?id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

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

            Device device = await _context.Devices.FindAsync(id);

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

            await _context.Entry(device).Reference(p => p.Owner).LoadAsync();

            if (!device.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(device.Owner) && p.IsAccepted.Equals(true)))
            {
                return(NotFound());
            }

            ViewBag.UserList = MakeAllowedUsersList(appUser, device.Owner.Id);

            DeviceCreateViewModel deviceViewModel = new DeviceCreateViewModel
            {
                Id                  = device.Id,
                Name                = device.Name,
                Comment             = device.Comment,
                OwnerId             = device.Owner.Id,
                IsServiceObligatory = device.IsServiceObligatory.GetValueOrDefault(),
                LastPlaceLeft       = device.LastPlaceLeft,
                LastServiceDate     = device.LastServiceDate,
                LastUsedDate        = device.LastUsedDate
            };

            return(View(deviceViewModel));
        }
Beispiel #19
0
        // GET: VoiceNotes
        public async Task <IActionResult> Index()
        {
            AppUser        appUser  = AuthExtender.GetLoggedInUser(this, _context);
            List <AppUser> allBound = _context.SickWardenRelations.Where(p => p.Warden.Equals(appUser) && p.IsAccepted.Equals(true)).Select(p => p.Sick).ToList();

            allBound.Add(appUser);

            List <VoiceNote> voiceNotes = await _context.VoiceNotes.Where(p => allBound.Contains(p.Owner)).ToListAsync();

            List <VoiceNoteIndexViewModel> voiceNoteIndexViewModels = new List <VoiceNoteIndexViewModel>();

            foreach (VoiceNote voiceNote in voiceNotes)
            {
                String content  = null;
                String fileName = voiceNote.FileName;
                if (fileName != null)
                {
                    String type   = GetTagType(fileName);
                    String base64 = await GetRecordingBase64(fileName);

                    if (base64 != null)
                    {
                        content = "data:" + type + ";base64," + base64;
                    }
                }

                voiceNoteIndexViewModels.Add(new VoiceNoteIndexViewModel
                {
                    Id                 = voiceNote.Id,
                    Name               = voiceNote.Name,
                    Comment            = voiceNote.Comment,
                    Owner              = voiceNote.Owner.FullName,
                    RecordingRawBase64 = content
                });
            }

            return(View(voiceNoteIndexViewModels));
        }
Beispiel #20
0
        // GET: Devices/Delete/5
        public async Task <IActionResult> Delete(long?id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

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

            Device device = await _context.Devices
                            .FirstOrDefaultAsync(m => m.Id == id);

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

            await _context.Entry(device).Reference(p => p.Owner).LoadAsync();

            if (!device.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(device.Owner) && p.IsAccepted.Equals(true)))
            {
                return(RedirectToAction(nameof(Index)));
            }

            DeviceIndexViewModel deviceViewModel = new DeviceIndexViewModel
            {
                Id                  = device.Id,
                Name                = device.Name,
                Comment             = device.Comment,
                Owner               = device.Owner.FullName,
                IsServiceObligatory = device.IsServiceObligatory.GetValueOrDefault(),
                LastPlaceLeft       = device.LastPlaceLeft,
                LastServiceDate     = device.LastServiceDate,
                LastUsedDate        = device.LastUsedDate
            };

            return(View(deviceViewModel));
        }
Beispiel #21
0
        public async Task <IActionResult> Edit(long id, DeviceCreateViewModel deviceViewModel)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            if (id != deviceViewModel.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                Device device = await _context.Devices.FindAsync(id);

                await _context.Entry(device).Reference(p => p.Owner).LoadAsync();

                if (!device.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(device.Owner) && p.IsAccepted.Equals(true)))
                {
                    return(NotFound());
                }

                if (!device.Owner.Id.Equals(deviceViewModel.OwnerId))
                {
                    AppUser newOwner = null;
                    if (appUser.Id.Equals(deviceViewModel.OwnerId))
                    {
                        newOwner = appUser;
                    }
                    else
                    {
                        AppUser probablyNewOwner = _context.Users.FirstOrDefault(p => p.Id.Equals(deviceViewModel.OwnerId));
                        if (probablyNewOwner == null)
                        {
                            ModelState.AddModelError("OwnerId", "Hack attempt!");
                            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                            return(View(deviceViewModel));
                        }

                        if (_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(probablyNewOwner) && p.IsAccepted.Equals(true)))
                        {
                            newOwner = probablyNewOwner;
                        }
                        else
                        {
                            ModelState.AddModelError("OwnerId", "Hack attempt!");
                            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                            return(View(deviceViewModel));
                        }
                    }
                    device.Owner = newOwner;
                }

                device.Name                = deviceViewModel.Name;
                device.Comment             = deviceViewModel.Comment;
                device.LastUsedDate        = deviceViewModel.LastUsedDate;
                device.LastPlaceLeft       = deviceViewModel.LastPlaceLeft;
                device.IsServiceObligatory = deviceViewModel.IsServiceObligatory;
                device.LastServiceDate     = deviceViewModel.LastServiceDate;

                try
                {
                    _context.Update(device);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!DeviceExists(device.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.UserList = MakeAllowedUsersList(appUser, deviceViewModel.OwnerId);
            return(View(deviceViewModel));
        }
Beispiel #22
0
        public async Task <IActionResult> Create(VoiceNoteCreateViewModel voiceNoteViewModel)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            if (ModelState.IsValid)
            {
                AppUser newOwner = null;
                if (appUser.Id.Equals(voiceNoteViewModel.OwnerId))
                {
                    newOwner = appUser;
                }
                else
                {
                    AppUser probablyNewOwner = _context.Users.FirstOrDefault(p => p.Id.Equals(voiceNoteViewModel.OwnerId));
                    if (probablyNewOwner == null)
                    {
                        ModelState.AddModelError("OwnerId", "Hack attempt!");
                        ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                        return(View(voiceNoteViewModel));
                    }

                    if (_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(probablyNewOwner) && p.IsAccepted.Equals(true)))
                    {
                        newOwner = probablyNewOwner;
                    }
                    else
                    {
                        ModelState.AddModelError("OwnerId", "Hack attempt!");
                        ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                        return(View(voiceNoteViewModel));
                    }
                }

                string newFileName = Guid.NewGuid().ToString() + Path.GetExtension(voiceNoteViewModel.File.FileName).ToLowerInvariant();
                if (voiceNoteViewModel != null && voiceNoteViewModel.File != null && voiceNoteViewModel.File.Length != 0)
                {
                    if (!Directory.Exists("wwwroot\\UserRecordings"))
                    {
                        Directory.CreateDirectory("wwwroot\\UserRecordings");
                    }

                    var path = Path.Combine(
                        Directory.GetCurrentDirectory(), "wwwroot\\UserRecordings",
                        newFileName);

                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await voiceNoteViewModel.File.CopyToAsync(stream);
                    }
                }
                else
                {
                    ModelState.AddModelError("File", "Invalid file!");
                    ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                    return(View(voiceNoteViewModel));
                }

                VoiceNote voiceNote = new VoiceNote
                {
                    Id       = voiceNoteViewModel.Id,
                    Name     = voiceNoteViewModel.Name,
                    Comment  = voiceNoteViewModel.Comment,
                    Owner    = newOwner,
                    FileName = newFileName
                };

                _context.Add(voiceNote);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
            return(View(voiceNoteViewModel));
        }
Beispiel #23
0
        public async Task <IActionResult> Edit(long id, MaterialCreateViewModel materialViewModel)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            if (id != materialViewModel.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                Material material = await _context.Materials.FindAsync(id);

                await _context.Entry(material).Reference(p => p.Owner).LoadAsync();

                if (!material.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(material.Owner) && p.IsAccepted.Equals(true)))
                {
                    return(NotFound());
                }

                if (!material.Owner.Id.Equals(materialViewModel.OwnerId))
                {
                    AppUser newOwner = null;
                    if (appUser.Id.Equals(materialViewModel.OwnerId))
                    {
                        newOwner = appUser;
                    }
                    else
                    {
                        AppUser probablyNewOwner = _context.Users.FirstOrDefault(p => p.Id.Equals(materialViewModel.OwnerId));
                        if (probablyNewOwner == null)
                        {
                            ModelState.AddModelError("OwnerId", "Hack attempt!");
                            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                            return(View(materialViewModel));
                        }

                        if (_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(probablyNewOwner) && p.IsAccepted.Equals(true)))
                        {
                            newOwner = probablyNewOwner;
                        }
                        else
                        {
                            ModelState.AddModelError("OwnerId", "Hack attempt!");
                            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                            return(View(materialViewModel));
                        }
                    }
                    material.Owner = newOwner;
                }

                material.Name          = materialViewModel.Name;
                material.Comment       = materialViewModel.Comment;
                material.LastUsedDate  = materialViewModel.LastUsedDate;
                material.LastPlaceLeft = materialViewModel.LastPlaceLeft;
                material.IsMedicine    = materialViewModel.IsMedicine;
                material.MedicineDose  = materialViewModel.MedicineDose;
                material.Amount        = materialViewModel.Amount;
                material.Type          = materialViewModel.Type;

                try
                {
                    _context.Update(material);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!MaterialExists(material.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.UserList = MakeAllowedUsersList(appUser, materialViewModel.OwnerId);
            return(View(materialViewModel));
        }