Exemplo n.º 1
0
        public async Task <ActionResult <Bug> > PostBug(BugDto bugDto)
        {
            var bug = _mapper.Map <Bug>(bugDto);

            bug.DateSubmitted = DateTime.Now;
            bug.DateTargeted  = DateTime.Now + TimeSpan.FromDays(14); // Default target of two weeks.
            bug.Status        = BugStatus.Open;

            _context.Bugs.Add(bug);
            await _context.SaveChangesAsync();

            return(Created(Request.Path.ToString() + "/" + bug.Id, bug));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PutBug([FromRoute] int id, [FromForm] BugDto defectdto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != defectdto.Id)
            {
                return(BadRequest());
            }

            var defecttoModify = _context.Defect.SingleOrDefault(c => c.Id == id);

            _mapper.Map <BugDto, Bug>(defectdto, defecttoModify);

            _context.Entry(defecttoModify).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }

            catch (DbUpdateConcurrencyException)
            {
                if (!DefectExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> PostDefect([FromForm] BugDto defectdto,
                                                     [FromForm(Name = "files")] ICollection <IFormFile> attachments,
                                                     [FromForm(Name = "notes")] ICollection <string> notes)
        {
            //string contentRootPath = Path.Combine(_hostingEnvironment.ContentRootPath , @"AppData\Attachments");

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var defecttoAdd = _mapper.Map <BugDto, Bug>(defectdto);

            _context.Defect.Add(defecttoAdd);
            await _context.SaveChangesAsync();

            if (attachments != null)
            {
                foreach (var file in attachments)
                {
                    if (file.Length > 0)
                    {
                        Guid uniqueFilename = Guid.NewGuid();
                        var  fileName       = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        fileName = fileName.Replace(".", uniqueFilename + ".");
                        await file.CopyToAsync(new FileStream(Path.Combine(contentRootPath, fileName), FileMode.CreateNew));

                        var DefectAttachmentToadd = new AttachmentDto
                        {
                            Path     = "AppData\\Attachments\\" + fileName,
                            FileName = file.FileName,
                            DefectId = defecttoAdd.Id
                        };
                        var attachmentToAdd = _mapper.Map <AttachmentDto, Attachment>(DefectAttachmentToadd);

                        _context.Attachment.Add(attachmentToAdd);
                        await _context.SaveChangesAsync();
                    }
                }
            }

            if (notes != null)
            {
                foreach (var note in notes)
                {
                    var NoteToAddDto = new NoteDto
                    {
                        AddedbyId   = defecttoAdd.LoggedbyId,
                        CreatedDate = DateTime.Now,
                        UpdatedDate = DateTime.Now,
                        Text        = note,
                        DefectId    = defecttoAdd.Id
                    };
                    var NoteToAdd = _mapper.Map <NoteDto, Note>(NoteToAddDto);
                    _context.Note.Add(NoteToAdd);
                    await _context.SaveChangesAsync();
                }
            }

            return(CreatedAtAction("GetDefectFromId", new { id = defecttoAdd.Id }, defecttoAdd));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> PatchBug([FromRoute] int id, [FromForm] BugDto defectdto,
                                                   [FromForm(Name = "attachmentstoAdd")] ICollection <IFormFile> attachments,
                                                   [FromForm(Name = "notes")] ICollection <string> noteslist,
                                                   [FromForm(Name = "attachmentlist")] ICollection <string> attachmentlist)
        {
            var GetDataClient = new HttpClient();
            ICollection <Attachment> CurrentAttachments = new Collection <Attachment>();
            ICollection <Note>       CurrentNotes       = new Collection <Note>();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != defectdto.Id)
            {
                return(BadRequest());
            }

            var defecttoModify = _context.Defect.SingleOrDefault(c => c.Id == id);

            _mapper.Map <BugDto, Bug>(defectdto, defecttoModify);

            if (defectdto.AssignToId == "0")
            {
                defecttoModify.AssignToId = null;
            }

            _context.Entry(defecttoModify).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }

            catch (DbUpdateConcurrencyException)
            {
                if (!DefectExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }


            GetDataClient.BaseAddress = new Uri(@"https://" + HttpContext.Request.Host.ToString());
            GetDataClient.DefaultRequestHeaders.Accept.Clear();
            GetDataClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(HttpContentTypes.Any));

            // HTTP GET
            var response = GetDataClient.GetAsync("/api/Attachments/BugId/" + id.ToString()).Result;

            if (response.IsSuccessStatusCode)
            {
                var data = response.Content.ReadAsAsync <IEnumerable <Attachment> >().Result;
                foreach (var attachment in data)
                {
                    CurrentAttachments.Add(attachment);
                }
            }

            foreach (Attachment attachment in CurrentAttachments)
            {
                var result = attachmentlist.FirstOrDefault(filename => filename == attachment.FileName);

                if (result == null)
                {
                    response = GetDataClient.DeleteAsync("/api/Attachments/" + attachment.Id.ToString()).Result;
                }
            }

            if (attachments != null)
            {
                foreach (var file in attachments)
                {
                    if (file.Length > 0)
                    {
                        Guid uniqueFilename = Guid.NewGuid();
                        var  fileName       = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        fileName = fileName.Replace(".", uniqueFilename + ".");
                        FileStream fs = new FileStream(Path.Combine(contentRootPath, fileName), FileMode.Create);
                        await file.CopyToAsync(fs);

                        fs.Close();

                        var DefectAttachmentToadd = new AttachmentDto
                        {
                            Path     = "AppData\\Attachments\\" + fileName,
                            FileName = file.FileName,
                            DefectId = defecttoModify.Id
                        };
                        var attachmentToAdd = _mapper.Map <AttachmentDto, Attachment>(DefectAttachmentToadd);

                        _context.Attachment.Add(attachmentToAdd);
                        await _context.SaveChangesAsync();
                    }
                }
            }

            // HTTP GET
            response = GetDataClient.GetAsync("/api/notes/BugId/" + id.ToString()).Result;

            if (response.IsSuccessStatusCode)
            {
                var data = response.Content.ReadAsAsync <IEnumerable <Note> >().Result;
                foreach (var note in data)
                {
                    CurrentNotes.Add(note);
                }
            }

            foreach (Note note in CurrentNotes)
            {
                var result = noteslist.FirstOrDefault(notesearch => notesearch == note.Text);

                if (result == null)
                {
                    response = GetDataClient.DeleteAsync("/api/notes/" + note.Id.ToString()).Result;
                }
            }

            if (noteslist != null)
            {
                foreach (var note in noteslist)
                {
                    var NoteToAddDto = new NoteDto
                    {
                        AddedbyId   = defecttoModify.LoggedbyId,
                        CreatedDate = DateTime.Now,
                        UpdatedDate = DateTime.Now,
                        Text        = note,
                        DefectId    = defecttoModify.Id
                    };
                    var NoteToAdd = _mapper.Map <NoteDto, Note>(NoteToAddDto);
                    _context.Note.Add(NoteToAdd);
                    await _context.SaveChangesAsync();
                }
            }

            return(NoContent());
        }