public IHttpActionResult CreateResearchProject(ResearchProjectDto researchProjectDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid form data."));
            }

            var researchProject = Mapper.Map <ResearchProjectDto, ResearchProject>(researchProjectDto);

            researchProject.InsertDate = DateTime.Now;

            using (var context = new CCFDataEntities())
            {
                context.ResearchProjects.Add(researchProject);
                context.SaveChanges();
            }

            researchProjectDto.ProjectID  = researchProject.ProjectID;
            researchProjectDto.InsertDate = researchProject.InsertDate.ToShortDateString();

            return(Created(new Uri(Request.RequestUri + "/" + researchProject.ProjectID), researchProjectDto));
        }
        public IHttpActionResult Update(int id, ResearchProjectDto researchProjectDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid form data."));
            }

            using (var context = new CCFDataEntities())
            {
                var researchProjectInDb = context.ResearchProjects.SingleOrDefault(p => p.ProjectID == id);

                if (researchProjectInDb == null)
                {
                    return(BadRequest("The selected research project could not be found."));
                }

                Mapper.Map(researchProjectDto, researchProjectInDb);
                Mapper.Map(researchProjectInDb, researchProjectDto);

                context.SaveChanges();
            }

            return(Ok(researchProjectDto));
        }
Esempio n. 3
0
        public List <RejectedAccessionDto> ValidateUploadedAccessions(List <string> accessions, CCFDataEntities context, ResearchProject researchProjectInDb)
        {
            List <RejectedAccessionDto> rejectedAccessions = new List <RejectedAccessionDto>();

            var projectId = researchProjectInDb.ProjectID;

            foreach (var accessionNumber in accessions)
            {
                RejectedAccessionDto rejectedAccession;
                // Ensure the accession number is an integer.
                if (!accessionNumber.All(char.IsDigit))
                {
                    rejectedAccession = new RejectedAccessionDto
                    {
                        Accession = accessionNumber,
                        Reason    = "Accession must be an integer."
                    };
                    rejectedAccessions.Add(rejectedAccession);
                    continue;
                }

                var convertedAccessionNumber = Convert.ToInt32(accessionNumber.Trim());

                var researchAccessionInDb = context.ResearchProjectAccessions
                                            .SingleOrDefault(a => a.ProjectID == projectId && a.Accession == convertedAccessionNumber);

                // Ensure the accession number has not already been added.
                if (researchAccessionInDb != null)
                {
                    rejectedAccession = new RejectedAccessionDto
                    {
                        Accession = accessionNumber,
                        Reason    = "Accession has already been added to the project."
                    };
                    rejectedAccessions.Add(rejectedAccession);
                    continue;
                }

                var globalAccessionInDb = context.GlobalAccessions
                                          .SingleOrDefault(a => a.Accession == convertedAccessionNumber);

                // Ensure the accession number exists globally.
                if (globalAccessionInDb == null)
                {
                    rejectedAccession = new RejectedAccessionDto
                    {
                        Accession = accessionNumber,
                        Reason    = "Accession does not exist in the global database."
                    };
                    rejectedAccessions.Add(rejectedAccession);
                    continue;
                }

                //accession passed validation, create an accession object to add to the database
                var accession = new ResearchProjectAccession()
                {
                    ResearchProject = researchProjectInDb,
                    Accession       = globalAccessionInDb.Accession,
                    AccessionGUID   = Guid.NewGuid(), //deidentified accession number
                    MRN             = globalAccessionInDb.MRN,
                    MRNGUID         = Guid.NewGuid()
                };

                context.ResearchProjectAccessions.Add(accession);
                context.SaveChanges();
            }

            return(rejectedAccessions);
        }