Ejemplo n.º 1
0
 public CreateModel(BasicDeskDbContext dbContext, UserManager <User> userManager, IMapper mapper)
 {
     this.Model       = new SolutionCreationBindingModel();
     this.dbContext   = dbContext;
     this.userManager = userManager;
     this.mapper      = mapper;
 }
Ejemplo n.º 2
0
 public CreateModel(ISolutionService solutionService, UserManager <User> userManager,
                    AttachmentService <SolutionAttachment> attachmentService, IFileUploader fileUploader, IAlerter alerter)
 {
     this.Model             = new SolutionCreationBindingModel();
     this.solutionService   = solutionService;
     this.userManager       = userManager;
     this.attachmentService = attachmentService;
     this.fileUploader      = fileUploader;
     this.alerter           = alerter;
 }
        public async Task <IActionResult> PostAsync(SolutionCreationBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            string userId = this.userManager.GetUserId(User);

            if (string.IsNullOrWhiteSpace(userId))
            {
                return(BadRequest());
            }

            var solution = new Solution
            {
                Title    = model.Title,
                Content  = model.Content,
                AuthorId = userId,
            };

            await this.solutionsService.AddAsync(solution);

            //if (model.Attachments != null)
            //{
            //    string path = await fileUploader.CreateAttachmentAsync(model.Subject, model.Attachments, "Requests");

            //    ICollection<RequestAttachment> attachments = new List<RequestAttachment>();

            //    foreach (var attachment in model.Attachments)
            //    {
            //        RequestAttachment requestAttachment = new RequestAttachment
            //        {
            //            FileName = attachment.FileName,
            //            PathToFile = Path.Combine(path, attachment.FileName),
            //            RequestId = request.Id
            //        };
            //        attachments.Add(requestAttachment);
            //    }

            //    await this.attachmentService.AddRangeAsync(attachments);
            //}

            await this.solutionsService.SaveChangesAsync();

            return(CreatedAtAction(nameof(this.PostAsync), model));
        }
Ejemplo n.º 4
0
        private async Task CreateAttachmentAsync(SolutionCreationBindingModel model, Solution solution)
        {
            string path       = Path.Combine(Directory.GetCurrentDirectory(), "Files", "Solutions", model.Attachment.FileName);
            var    fileStream = new FileStream(path, FileMode.Create);

            using (fileStream)
            {
                await model.Attachment.CopyToAsync(fileStream);
            }

            this.dbContext.SolutionAttachments.Add(new SolutionAttachment
            {
                FileName   = model.Attachment.FileName,
                PathToFile = path,
                SolutionId = solution.Id
            });
        }