コード例 #1
0
        public async Task <SearchIssueDetailsViewModel> GetIssueDetailsAsync(User user, SearchIssueDetailFormModel searchIssueDetailForm)
        {
            try
            {
                var searchIssueDetailsViewModel = new SearchIssueDetailsViewModel();
                var querable = _context.IssueDetails.Include(c => c.User).Include(c => c.IssueDetailComments).Include(c => c.IssueDetailAttachments).Include(c => c.IssueDetailLikes).Include(c => c.EquipmentAttachmentIssueDetailSubscriptions).Where(c => c.IssueId == searchIssueDetailForm.IssueId).AsQueryable();
                if (searchIssueDetailForm.IssueDetailsId.HasValue)
                {
                    querable = querable.Where(c => c.Id == searchIssueDetailForm.IssueDetailsId);
                }
                var resp = await querable.ToListAsync();

                if (resp is null)
                {
                    throw new ExperienceManagementGlobalException(IssueServiceErrors.IssueNotFoundError);
                }
                var result         = _context.Issues.Include(c => c.EquipmentIssueSubscriptions).FirstOrDefault(c => c.Id == searchIssueDetailForm.IssueId);
                var issueviewmodel = new IssueViewModel();
                if (result != null)
                {
                    issueviewmodel.Id           = result.Id;
                    issueviewmodel.AnswerCount  = result.AnswerCounts;
                    issueviewmodel.CreationDate = result.CreationDate;
                    issueviewmodel.Description  = result.Description;
                    issueviewmodel.Equipments   = result.EquipmentIssueSubscriptions.Select(x => new EquipmentViewModel()
                    {
                        Description   = x.Equipment.Description,
                        EquipmentName = x.Equipment.EquipmentName,
                        Id            = x.EquipmentId
                    }).ToList();
                    issueviewmodel.IsActive       = result.IsActive;
                    issueviewmodel.SumLikes       = resp.Sum(c => c.Likes);
                    issueviewmodel.IssueType      = result.IssueType;
                    issueviewmodel.LastUpdateDate = result.LastUpdateDate;
                    issueviewmodel.Title          = result.Title;
                    issueviewmodel.UserId         = result.UserId;
                    issueviewmodel.UserName       = result.User.Username;
                    issueviewmodel.FileId         = result.FileId;
                }



                var issueDetailViewModel = new List <IssueDetailViewModel>();
                resp.ForEach(response =>
                {
                    var issuedetail = new IssueDetailViewModel()
                    {
                        Caption                       = response.Caption,
                        CreationDate                  = response.CreationDate,
                        Text                          = response.Text,
                        IsCorrectAnswer               = response.IsCorrectAnswer,
                        IssueId                       = response.IssueId,
                        Likes                         = response.Likes,
                        IsUserLike                    = response.IssueDetailLikes.Any(d => d.UserId == user.Id),
                        IssueDetailId                 = response.Id,
                        UserId                        = response.UserId,
                        UserName                      = response.User.Username,
                        LastUpdateDate                = response.LastUpdateDate,
                        IssueDetailComments           = new List <IssueDetailCommentViewModel>(),
                        IssueDetailAttachments        = new List <IssueDetailAttachmentViewModel>(),
                        EquipmentAttachmentViewModels = new List <EquipmentAttachmentViewModel>()
                    };
                    response.IssueDetailComments.ToList().ForEach(c =>
                    {
                        var IssueDetailComment = new IssueDetailCommentViewModel()
                        {
                            Comment       = c.Comment,
                            CreationDate  = c.CreationDate,
                            IssueDetailId = c.IssueDetailId,
                            Id            = c.Id,
                            UserId        = c.UserId,
                            UserName      = c.User.Username
                        };
                        issuedetail.IssueDetailComments.Add(IssueDetailComment);
                    });
                    response.IssueDetailAttachments.ToList().ForEach(c =>
                    {
                        var IssueDetailAtt = new IssueDetailAttachmentViewModel()
                        {
                            FileSize      = c.FileSize,
                            FileType      = c.FileType,
                            FileId        = c.FileId,
                            ContentType   = ContentType.GetContentType($".{c.FileType}"),
                            IssueDetailId = c.IssueDetailId,
                            Id            = c.Id,
                        };
                        issuedetail.IssueDetailAttachments.Add(IssueDetailAtt);
                    });
                    response.EquipmentAttachmentIssueDetailSubscriptions?.ToList().ForEach(c =>
                    {
                        var equipmentAttachment = new EquipmentAttachmentViewModel()
                        {
                            EquipmentAttachmentId   = c.Id,
                            CreationDate            = c.EquipmentAttachment.CreationDate,
                            Description             = c.EquipmentAttachment.Description,
                            EquipmentAttachmentName = c.EquipmentAttachment.EquipmentAttachmentName,
                            EquipmentId             = c.EquipmentAttachment.EquipmentId,
                            EquipmentAttachmentType = c.EquipmentAttachment.EquipmentAttachmentType,
                            FileId   = c.EquipmentAttachment.FileId,
                            FileSize = c.EquipmentAttachment.FileSize,
                            FileType = c.EquipmentAttachment.FileType
                        };
                        issuedetail.EquipmentAttachmentViewModels.Add(equipmentAttachment);
                    });

                    issueDetailViewModel.Add(issuedetail);
                });
                searchIssueDetailsViewModel.issueDetailViewModel = issueDetailViewModel;
                searchIssueDetailsViewModel.IssueViewModel       = issueviewmodel;
                return(searchIssueDetailsViewModel);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(IssueServiceErrors.GetIssueDetailsError, ex);
            }
        }
コード例 #2
0
        public async Task <IssueDetailViewModel> AddIssueDetailAsync(User user, AddIssueDetailFormModel issueDetailForm)
        {
            try
            {
                var issue = await _context.Issues.FindAsync(issueDetailForm.IssueId);

                if (issue is null)
                {
                    throw new ExperienceManagementGlobalException(IssueServiceErrors.IssueNotFoundError);
                }
                var userReq = await _context.Users.FindAsync(issueDetailForm.UserId);

                if (userReq is null)
                {
                    throw new ExperienceManagementGlobalException(UsersServiceErrors.UserNotFoundError);
                }
                var issueDetail = new IssueDetail()
                {
                    IssueId        = issue.Id,
                    CreationDate   = DateTime.Now,
                    Caption        = issueDetailForm.Caption,
                    Text           = issueDetailForm.Text,
                    UserId         = issueDetailForm.UserId,
                    LastUpdateDate = DateTime.Now
                };
                issue.AnswerCounts   = issue.AnswerCounts + 1;
                issue.LastUpdateDate = DateTime.Now;
                _context.Issues.Update(issue);

                await _context.IssueDetails.AddAsync(issueDetail);

                if (issueDetailForm.UploadFiles != null && issueDetailForm.UploadFiles.Count > 0)
                {
                    var notFoundFiles = 0;
                    var filesAddress  = new List <FileAddress>();
                    issueDetailForm.UploadFiles.ForEach(uf =>
                    {
                        var fileAddress = _context.FileAddresses.Find(uf);
                        if (fileAddress == null)
                        {
                            notFoundFiles++;
                        }
                        filesAddress.Add(fileAddress);
                    });
                    if (notFoundFiles > 0)
                    {
                        throw new ExperienceManagementGlobalException(IssueServiceErrors.NotExistFileAddressError);
                    }

                    filesAddress.ForEach(async fa =>
                    {
                        var issueDetailAttachment = new IssueDetailAttachment()
                        {
                            IssueDetails = issueDetail,
                            FileId       = fa.FileId,
                            FileSize     = fa.FileSize,
                            FileType     = fa.FileType,
                        };
                        await _context.IssueDetailAttachments.AddAsync(issueDetailAttachment);
                    });
                }

                if (issueDetailForm.EquipmentAttachmentIds != null && issueDetailForm.EquipmentAttachmentIds.Count > 0)
                {
                    issueDetailForm.EquipmentAttachmentIds.ForEach(async EqId =>
                    {
                        var eqAttDetailSubscription = new EquipmentAttachmentIssueDetailSubscription()
                        {
                            IssueDetail           = issueDetail,
                            EquipmentAttachmentId = EqId
                        };
                        await _context.EquipmentAttachmentIssueDetailSubscriptions.AddAsync(eqAttDetailSubscription);
                    });
                }


                await _context.SaveChangesAsync();


                var issueDetailsViewM = new IssueDetailViewModel()
                {
                    Caption                       = issueDetail.Caption,
                    CreationDate                  = issueDetail.CreationDate,
                    Text                          = issueDetail.Text,
                    IsCorrectAnswer               = issueDetail.IsCorrectAnswer,
                    IssueId                       = issueDetail.IssueId,
                    Likes                         = issueDetail.Likes,
                    IssueDetailId                 = issueDetail.Id,
                    UserId                        = issueDetail.UserId,
                    UserName                      = issueDetail.User.Username,
                    LastUpdateDate                = issueDetail.LastUpdateDate,
                    IssueDetailComments           = new List <IssueDetailCommentViewModel>(),
                    IssueDetailAttachments        = new List <IssueDetailAttachmentViewModel>(),
                    EquipmentAttachmentViewModels = new List <EquipmentAttachmentViewModel>()
                };
                issueDetail.IssueDetailComments?.ToList().ForEach(c =>
                {
                    var IssueDetailComment = new IssueDetailCommentViewModel()
                    {
                        Comment       = c.Comment,
                        CreationDate  = c.CreationDate,
                        IssueDetailId = c.IssueDetailId,
                        Id            = c.Id,
                        UserId        = c.UserId,
                        UserName      = c.User.Username
                    };
                    issueDetailsViewM.IssueDetailComments.Add(IssueDetailComment);
                });
                issueDetail.IssueDetailAttachments?.ToList().ForEach(c =>
                {
                    var IssueDetailAtt = new IssueDetailAttachmentViewModel()
                    {
                        FileSize      = c.FileSize,
                        FileType      = c.FileType,
                        FileId        = c.FileId,
                        ContentType   = ContentType.GetContentType($".{c.FileType}"),
                        IssueDetailId = c.IssueDetailId,
                        Id            = c.Id,
                    };
                    issueDetailsViewM.IssueDetailAttachments.Add(IssueDetailAtt);
                });

                if (issueDetail.EquipmentAttachmentIssueDetailSubscriptions != null)
                {
                    var EqIds = issueDetail.EquipmentAttachmentIssueDetailSubscriptions.Select(c => c.Id).ToList();
                    var resp  = await _context.EquipmentAttachmentIssueDetailSubscriptions.Include(c => c.EquipmentAttachment).Where(c => EqIds.Contains(c.Id)).ToListAsync();

                    resp?.ToList().ForEach(c =>
                    {
                        var equipmentAttachment = new EquipmentAttachmentViewModel()
                        {
                            EquipmentAttachmentId   = c.Id,
                            CreationDate            = c.EquipmentAttachment.CreationDate,
                            Description             = c.EquipmentAttachment.Description,
                            EquipmentAttachmentName = c.EquipmentAttachment.EquipmentAttachmentName,
                            EquipmentId             = c.EquipmentAttachment.EquipmentId,
                            EquipmentAttachmentType = c.EquipmentAttachment.EquipmentAttachmentType,
                            FileId   = c.EquipmentAttachment.FileId,
                            FileSize = c.EquipmentAttachment.FileSize,
                            FileType = c.EquipmentAttachment.FileType
                        };
                        issueDetailsViewM.EquipmentAttachmentViewModels.Add(equipmentAttachment);
                    });
                }


                return(issueDetailsViewM);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(IssueServiceErrors.AddIssueDetailError, ex);
            }
        }