示例#1
0
        public async Task <IActionResult> UpdateGroup([FromForm] GroupModel model)
        {
            try
            {
                if (this.ValidRoleForAction(_context, _auth, new string[] { "Editor" }))
                {
                    if (ModelState.IsValid)
                    {
                        PhotoUploadCloudinary upload = new PhotoUploadCloudinary(_cloudinaryConfig);
                        Photo photo = upload.Upload(model.Photo);

                        Group item = await _context.GetByIdAsync <Group>(x => x.Id == model.Id);

                        if (item != null)
                        {
                            item.FacultyId       = model.FacultyId;
                            item.LessonHourId    = model.LessonHourId;
                            item.LessonStartDate = model.LessonStartDate;
                            item.LessonEndDate   = model.LessonEndDate;
                            item.LessonStatusId  = model.LessonStatusId;
                            item.Name            = model.Name;
                            item.Photo           = photo;
                            item.RoomId          = model.RoomId;

                            Teacher teacher = await _context.GetByIdAsync <Teacher>(x => x.Id == model.TeacherId);

                            if (teacher != null)
                            {
                                TeacherGroup teacherGroup = new TeacherGroup {
                                    Group = item, Teacher = teacher
                                };
                                await _context.Add(teacherGroup);
                            }

                            _context.Update(item);
                            if (await _context.SaveAll())
                            {
                                return(Ok(item));
                            }
                            return(BadRequest("Error updating group"));
                        }
                        return(NotFound("Group not found"));
                    }
                    return(BadRequest("Model is not valid"));
                }
                return(Forbid());
            }
            catch (Exception ex)
            {
                var arguments = this.GetBaseData(_context, _auth);
                _logger.LogException(ex, arguments.Email, arguments.Path);
                return(BadRequest($"{ex.GetType().Name} was thrown."));
            }
        }
        public async Task <IActionResult> Update([FromForm] LeftNavItemModel model)
        {
            try
            {
                if (this.ValidRoleForAction(_context, _auth, new string[] { "Admin" }))
                {
                    bool saved;
                    if (ModelState.IsValid)
                    {
                        LeftNavItem item = await _context.GetByIdAsync <LeftNavItem>(x => x.Id == model.Id);

                        PhotoUploadCloudinary upload = new PhotoUploadCloudinary(_cloudinaryConfig);
                        Photo photo = upload.Upload(model.Photo);

                        item.Name          = model.Name;
                        item.RouterLink    = model.RouterLink;
                        item.IconClassname = model.IconClassname;
                        item.IsVisible     = model.IsVisible;
                        item.Photo         = photo;

                        await _context.Add(photo);

                        _context.Update(item);
                        saved = await _context.SaveAll();

                        if (saved == true)
                        {
                            return(Ok(item));
                        }
                        else
                        {
                            return(BadRequest("Item cannot be updated"));
                        }
                    }
                    return(BadRequest("Model is not valid"));
                }
                return(Forbid());
            }
            catch (Exception ex)
            {
                var arguments = this.GetBaseData(_context, _auth);
                _logger.LogException(ex, arguments.Email, arguments.Path);
                return(BadRequest($"{ex.GetType().Name} was thrown."));
            }
        }
示例#3
0
        public async Task <IActionResult> Add([FromForm] FacultyModel model)
        {
            try
            {
                if (this.ValidRoleForAction(_context, _auth, new string[] { "Admin" }))
                {
                    bool saved;
                    if (ModelState.IsValid)
                    {
                        PhotoUploadCloudinary upload = new PhotoUploadCloudinary(_cloudinaryConfig);
                        Photo photo = upload.Upload(model.Photo);

                        Faculty item = new Faculty
                        {
                            Name       = model.Name,
                            LessonHour = model.LessonHour,
                            Photo      = photo
                        };

                        await _context.Add(photo);

                        await _context.Add(item);

                        saved = await _context.SaveAll();

                        if (saved == true)
                        {
                            return(Ok(item));
                        }
                    }
                    return(BadRequest("Model is not valid"));
                }
                return(Forbid());
            }
            catch (Exception ex)
            {
                var arguments = this.GetBaseData(_context, _auth);
                _logger.LogException(ex, arguments.Email, arguments.Path);
                return(BadRequest($"{ex.GetType().Name} was thrown."));
            }
        }
示例#4
0
        public async Task <IActionResult> Update([FromForm] BookModel model)
        {
            try
            {
                if (this.ValidRoleForAction(_context, _auth, new string[] { "Student", "Teacher", "Editor" }))
                {
                    AppIdentityUser currentuser = this.GetLoggedUser(_auth, _context);
                    if (model.UserId == currentuser.Id || await _auth.CheckUserRole(currentuser, "Editor"))
                    {
                        if (ModelState.IsValid)
                        {
                            string[]   tags     = model.Tags.Split(',');
                            List <Tag> bookTags = new List <Tag>();
                            foreach (var tag in tags)
                            {
                                Tag t = await _context.GetByNameAsync <Tag>(x => x.Name.ToLower() == tag.Trim().ToLower());

                                bookTags.Add(t);
                            }

                            Book item = await _context.GetByIdAsync <Book>(x => x.Id == model.Id);

                            PhotoUploadCloudinary upload = new PhotoUploadCloudinary(_cloudinaryConfig);
                            Photo photo = upload.Upload(model.Book);

                            PdfUploadCloudinary pdfUpload = new PdfUploadCloudinary(_cloudinaryConfig);
                            File file = pdfUpload.Upload(model.Book);

                            item.Name       = model.Name;
                            item.Author     = model.Author;
                            item.Year       = model.Year;
                            item.File       = file;
                            item.Photo      = photo;
                            item.LanguageId = model.LanguageId;
                            item.Pages      = model.Pages;
                            item.FacultyId  = model.FacultyId;
                            item.Photo      = photo;

                            List <PostTag> newTagPosts = new List <PostTag>();
                            foreach (var tag in bookTags)
                            {
                                newTagPosts.Add(new PostTag()
                                {
                                    Post = item, Tag = tag
                                });
                            }

                            List <PostTag> oldTagPosts = await _context.GetPostTags(item);

                            foreach (var tp in oldTagPosts)
                            {
                                _context.Delete(tp);
                            }

                            item.PostTags = newTagPosts;

                            await _context.Add(photo);

                            _context.Update(item);
                            bool saved = await _context.SaveAll();

                            if (saved == true)
                            {
                                if (item.AppIdentityUser.UserType == "Student")
                                {
                                    BookViewModel viewModel = new BookViewModel(item);
                                    viewModel.GroupName = _context.GetUserGroup(item.AppIdentityUserId);
                                    return(Ok(viewModel));
                                }
                                return(Ok(new BookViewModel(item)));
                            }
                            else
                            {
                                return(BadRequest("Item cannot be updated"));
                            }
                        }
                        return(BadRequest("Model is not valid"));
                    }
                    return(BadRequest($"{currentuser.Name}, you don't have a permission"));
                }
                return(Forbid());
            }
            catch (Exception ex)
            {
                var arguments = this.GetBaseData(_context, _auth);
                _logger.LogException(ex, arguments.Email, arguments.Path);
                return(BadRequest($"{ex.GetType().Name} was thrown."));
            }
        }
示例#5
0
        public async Task <IActionResult> Add([FromForm] BookModel model)
        {
            try
            {
                if (this.ValidRoleForAction(_context, _auth, new string[] { "Student", "Teacher" }))
                {
                    bool saved;
                    if (ModelState.IsValid)
                    {
                        string[]   tags     = model.Tags.Split(',');
                        List <Tag> bookTags = new List <Tag>();
                        foreach (var tag in tags)
                        {
                            Tag t = await _context.GetByNameAsync <Tag>(x => x.Name.ToLower() == tag.Trim().ToLower());

                            bookTags.Add(t);
                        }

                        PhotoUploadCloudinary upload = new PhotoUploadCloudinary(_cloudinaryConfig);
                        Photo photo = upload.Upload(model.Cover);

                        PdfUploadCloudinary pdfUpload = new PdfUploadCloudinary(_cloudinaryConfig);
                        File file = pdfUpload.Upload(model.Book);

                        Book item = new Book
                        {
                            Name            = model.Name,
                            AppIdentityUser = this.GetLoggedUser(_auth, _context),
                            Photo           = photo,
                            File            = file,
                            Author          = model.Author,
                            Language        = await _context.GetByIdAsync <Language>(x => x.Id == model.LanguageId),
                            Year            = model.Year,
                            Pages           = model.Pages,
                            Faculty         = await _context.GetByIdAsync <Faculty>(x => x.Id == model.FacultyId)
                        };

                        List <PostTag> tagPosts = new List <PostTag>();
                        foreach (var tag in bookTags)
                        {
                            tagPosts.Add(new PostTag()
                            {
                                Post = item, Tag = tag
                            });
                        }

                        item.PostTags = tagPosts;

                        await _context.Add(photo);

                        await _context.Add(item);

                        await _context.SaveAll();

                        var broadcast = new Notifier(_context, _auth);
                        await _context.AddRange(await broadcast.NewBook(item));

                        saved = await _context.SaveAll();

                        if (saved == true)
                        {
                            if (item.AppIdentityUser.UserType == "Student")
                            {
                                BookViewModel viewModel = new BookViewModel(item);
                                viewModel.GroupName = _context.GetUserGroup(item.AppIdentityUserId);
                                return(Ok(viewModel));
                            }
                            return(Ok(new BookViewModel(item)));
                        }
                    }
                    return(BadRequest("Model is not valid"));
                }
                return(Forbid());
            }
            catch (Exception ex)
            {
                var arguments = this.GetBaseData(_context, _auth);
                _logger.LogException(ex, arguments.Email, arguments.Path);
                return(BadRequest($"{ex.GetType().Name} was thrown."));
            }
        }