Exemple #1
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."));
            }
        }
Exemple #2
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."));
            }
        }