Exemple #1
0
 public ClientComicTag(ComicTag tag)
 {
     this.Uid      = tag.Uid;
     this.ComicId  = tag.ComicId;
     this.X        = tag.X;
     this.Y        = tag.Y;
     this.Nickname = tag.User.Nickname;
 }
Exemple #2
0
        public async Task <IActionResult> UploadComic(IFormFile file, string title, string tags, string description)
        {
            if (file == null || file.Length == 0)
            {
                ModelState.AddModelError("File", "File not selected!");
                return(View());
            }

            var fileExtension = file.FileName.Substring(file.FileName.Length - Math.Min(4, file.FileName.Length));

            if (fileExtension != ".jpg" && fileExtension != ".png")
            {
                ModelState.AddModelError("File", "File not in the right format!");
                return(View());
            }

            var username = this.ControllerContext.HttpContext.User.Identity.Name;

            var           path         = "_Pictures\\" + username + "\\Comic";
            DirectoryInfo di           = Directory.CreateDirectory(path);
            var           pathToImages = path + "\\" + Guid.NewGuid() + fileExtension;

            using (var stream = new FileStream(pathToImages, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            var picture = new Picture()
            {
                UploadDate = DateTime.Now,
                Url        = pathToImages,
                UserName   = username,
                UserId     = userId,
                IsImage    = false,
            };

            var keywords = tags
                           .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                           .ToList();

            var listOfTagsAll = this.Context.Tags;

            var listOfComicTags = new List <ComicTag>();

            var comic = new Comic
            {
                Title         = title,
                Cover         = picture,
                CoverId       = picture.Id,
                Description   = description,
                UploadDate    = DateTime.Now,
                OwnerUsername = username
            };

            foreach (var tag in keywords)
            {
                var existsTag = listOfTagsAll.Where(a => a.TagName == tag).FirstOrDefault();

                if (existsTag == null)
                {
                    var newTag = new Tag
                    {
                        TagName = tag
                    };

                    var comicTag = new ComicTag
                    {
                        Comic   = comic,
                        ComicId = comic.Id,
                        Tag     = newTag,
                        TagId   = newTag.Id
                    };

                    listOfComicTags.Add(comicTag);

                    this.Context.ComicTags.Add(comicTag);
                    this.Context.Tags.Add(newTag);
                }
                else
                {
                    var pictureTag = new ComicTag
                    {
                        Comic   = comic,
                        ComicId = comic.Id,
                        Tag     = existsTag,
                        TagId   = existsTag.Id
                    };

                    listOfComicTags.Add(pictureTag);

                    this.Context.ComicTags.Add(pictureTag);
                }
            }

            //picture.Tags = listOfComicTags;
            comic.Tags = listOfComicTags;

            this.Context.Pictures.Add(picture);
            this.Context.Comics.Add(comic);
            await this.Context.SaveChangesAsync();

            return(Redirect($"/comics/details/{comic.Id}"));
        }