コード例 #1
0
        public async Task <IActionResult> CreateNewResearch(ResearchViewModel researchView)
        {
            if (ModelState.IsValid)
            {
                var researchDto    = mapper.Map <ResearchDTO>(researchView);
                var stringedUserId = HttpContext.Session.GetString("userId");
                if (!string.IsNullOrEmpty(stringedUserId))
                {
                    researchDto.AppUser = await accountManager.FindByIdAsync(stringedUserId);
                }
                else
                {
                    researchDto.AppUser = await accountManager.GetUserAsync(User);
                }

                var research = await researchService.СreateNewResearch(researchDto);

                if (researchView.Tags != null)
                {
                    var collection = researchView.Tags.Split(',')
                                     .ToList()
                                     .Select(name => new Tag {
                        TagName = name
                    });

                    foreach (var item in collection)
                    {
                        var tag = await tagService.FindTag(item.TagName);

                        if (tag is null)
                        {
                            tag = await tagService.AddTag(item);
                        }
                        await tagService.AttachTag(tag, research);
                    }
                }
                return(RedirectToAction(nameof(ResearchDetails), new { id = research.Id.ToString() }));
            }

            return(View());
        }