コード例 #1
0
        public async Task <IActionResult> CreateDocument(DocumentInputModel model)
        {
            this.FillServiceUnifiedModel();
            if (!this.ModelState.IsValid)
            {
                return(this.View("Create", this.unifiedModel));
            }

            var document = new CreateDocumentServiceModel
            {
                Name        = model.DocumentName,
                Description = model.DocumentDescription,
            };

            await this.documentsService.CreateAsync(document);

            return(this.RedirectToAction("Create"));
        }
コード例 #2
0
        //--------------- METHODS -----------------
        /// <summary>
        /// Creates a new <see cref="Document"/> using the <see cref="CreateDocumentServiceModel"/>.
        /// If such <see cref="Document"/> already exists in the database, fetches it's (int)<c>Id</c> and returns it.
        /// If such <see cref="Document"/> doesn't exist in the database, adds it and return it's (int)<c>Id</c>.
        /// </summary>
        /// <param name="model">Service model with <c>Name</c> and <c>Description</c></param>
        /// <returns>Document ID</returns>
        public async Task <int> CreateAsync(CreateDocumentServiceModel model)
        {
            int documentId = this.dbContext.Documents.Where(d => d.Name == model.Name)
                             .Select(x => x.Id)
                             .FirstOrDefault();

            if (documentId != 0)   // If documentId is different than 0 (int default value), document with such name already exists, so return it's id.
            {
                return(documentId);
            }

            Document document = new Document
            {
                Name        = model.Name,
                Description = model.Description,
            };

            await this.dbContext.Documents.AddAsync(document);

            await this.dbContext.SaveChangesAsync();

            return(document.Id);
        }