Example #1
0
        public void DocumentUpdateDtoProperlyReversedToDocumentUpdate()
        {
            DocumentUpdateDto upDto1 = new DocumentUpdateDto
            {
                Id                  = 1,
                DocumentId          = 1,
                IsPublished         = true,
                LatestTopicsUpdated = "This is the first version of the document.",
                Timestamp           = DateTime.Today
            };

            DocumentUpdate up1 = new DocumentUpdate
            {
                Id                  = 1,
                DocumentId          = 1,
                IsPublished         = true,
                LatestTopicsUpdated = "This is the first version of the document.",
                Timestamp           = DateTime.Today
            };

            DocumentUpdate a2 = _mapper.Map <DocumentUpdate>(upDto1);

            Assert.NotNull(a2);
            Assert.True(up1.Equals(a2));
            Assert.True(up1.Equals(a2, true));
        }
Example #2
0
        public void PostDocumentsDocumentIdTest()
        {
            // TODO: add unit test for the method 'PostDocumentsDocumentId'
            string         documentId = null; // TODO: replace null with proper value
            DocumentUpdate body       = null; // TODO: replace null with proper value
            string         expand     = null; // TODO: replace null with proper value
            bool?          _override  = null; // TODO: replace null with proper value
            var            response   = instance.PostDocumentsDocumentId(documentId, body, expand, _override);

            Assert.IsInstanceOf <Document> (response, "response is Document");
        }
        public async void DeleteDocumentUpdateIdDeletesDocumentUpdate()
        {
            using (var context = DbTestContext.GenerateContextWithData())
                using (var controller = new DocumentsController(context, _mapper))
                {
                    DocumentUpdate dup = context.DocumentUpdates.AsNoTracking().Last();

                    var result = await controller.DeleteDocumentUpdate(dup.DocumentId, dup.Id);

                    DocumentUpdateDto a3 = _mapper.Map <DocumentUpdateDto>(dup);

                    Assert.NotNull(result);
                    var okObjectResult = Assert.IsType <OkObjectResult>(result);
                    var resultValue    = okObjectResult.Value;
                    Assert.Equal(a3, resultValue);
                }
        }
Example #4
0
        public async Task <IActionResult> PostDocumentUpdate([FromRoute] int documentId, [FromBody] DocumentUpdateDto documentUpdate)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (documentId != documentUpdate.DocumentId)
            {
                ModelState.AddModelError("Invalid Document ID", "The Document ID supplied in the query and the body of the request do not match.");
                return(BadRequest(ModelState));
            }

            if (!DocumentExists(documentId))
            {
                ModelState.AddModelError("Invalid Document ID", "The Document ID supplied in the does not match an existing document.");
                return(BadRequest(ModelState));
            }

            if (documentUpdate.Timestamp.Year == 1)
            {
                documentUpdate.Timestamp = DateTime.UtcNow;
            }

            DocumentUpdate reversedUpdate = _mapper.Map <DocumentUpdate>(documentUpdate);


            _context.DocumentUpdates.Add(reversedUpdate);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception xpc)
            {
                ModelState.AddModelError("Error", xpc.ToString());
                return(BadRequest(ModelState));
            }

            return(CreatedAtAction("GetDocumentUpdate", new { documentId = reversedUpdate.DocumentId, updateId = reversedUpdate.Id }, _mapper.Map <DocumentUpdateDto>(reversedUpdate)));
        }
Example #5
0
 public void Init()
 {
     instance = new DocumentUpdate();
 }
Example #6
0
        public async Task <IActionResult> PutDocument([FromRoute] int id, [FromBody] DocumentDto document)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != document.Id)
            {
                ModelState.AddModelError("Invalid ID", "The ID supplied in the query and the body of the request do not match.");
                return(BadRequest(ModelState));
            }

            Document originalDocument = _context.Documents
                                        .Include(d => d.DocumentAuthors)
                                        .Include(d => d.DocumentCatalogs)
                                        .Include(d => d.Updates)
                                        .SingleOrDefault(d => d.Id == id);

            if (document.RowVersion != originalDocument.RowVersion)
            {
                ModelState.AddModelError("Concurency Issue", "The record has been modified by another user. Please try again.");
                return(BadRequest(ModelState));
            }

            originalDocument.Title            = document.Title;
            originalDocument.ShortDescription = document.ShortDescription;
            originalDocument.IsFitForClients  = document.IsFitForClients;

            originalDocument.HtmlLink = document.HtmlLink;
            originalDocument.PdfLink  = document.PdfLink;
            originalDocument.WordLink = document.WordLink;

            originalDocument.AitId            = document.AitId;
            originalDocument.ProductVersionId = document.ProductVersionId;
            originalDocument.DocumentTypeId   = document.DocumentTypeId;


            // Handle Document Update
            DocumentUpdate update = new DocumentUpdate
            {
                DocumentId          = id,
                IsPublished         = document.IsPublished,
                LatestTopicsUpdated = document.LatestTopicsUpdated,
                Timestamp           = DateTime.UtcNow
            };

            if (originalDocument.Updates == null)
            {
                originalDocument.Updates = new List <DocumentUpdate> {
                    update
                };
            }
            else
            {
                originalDocument.Updates.Add(update);
            }


            // Handle Document Authors
            // Add new authors
            foreach (int authorId in document.DocumentAuthorIds)
            {
                var daList = originalDocument.DocumentAuthors.Where(da => da.AuthorId == authorId);

                if (daList != null && daList.Count() == 0)
                {
                    originalDocument.DocumentAuthors.Add(new DocumentAuthor
                    {
                        AuthorId   = authorId,
                        DocumentId = id
                    });
                }
            }

            // Remove authors
            var originalAuthorIds = originalDocument.DocumentAuthors.Select(da => da.AuthorId);

            foreach (int origDAid in originalAuthorIds.ToList())
            {
                if (document.DocumentAuthorIds.Contains(origDAid))
                {
                    continue;
                }

                originalDocument.DocumentAuthors.Remove(originalDocument.DocumentAuthors.Single(da => da.AuthorId == origDAid));
            }

            // Handle Document Catalogs
            // Add new catalogs
            foreach (int catId in document.DocumentCatalogIds)
            {
                var dcList = originalDocument.DocumentCatalogs.Where(dc => dc.CatalogId == catId);

                if (dcList != null && dcList.Count() == 0)
                {
                    originalDocument.DocumentCatalogs.Add(new DocumentCatalog
                    {
                        CatalogId  = catId,
                        DocumentId = id
                    });
                }
            }

            // Remove catalogs
            var originalCatIds = originalDocument.DocumentCatalogs.Select(dc => dc.CatalogId);

            foreach (int origCatId in originalCatIds.ToList())
            {
                if (document.DocumentCatalogIds.Contains(origCatId))
                {
                    continue;
                }

                originalDocument.DocumentCatalogs.Remove(originalDocument.DocumentCatalogs.Single(dc => dc.CatalogId == origCatId));
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException xcp)
            {
                if (!DocumentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    ModelState.AddModelError("Error", xcp.ToString());
                    return(BadRequest(ModelState));
                }
            }

            return(NoContent());
        }