/// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        static public GlobalVars.ResultGeneric NewProject(GlobalVars.Project project)
        {
            GlobalVars.ResultGeneric result = new GlobalVars.ResultGeneric()
            {
                ReturnCode     = 0,
                Message        = "",
                RecordsCount   = 0,
                HttpStatusCode = ""
            };
            try
            {
                logger.Trace("Entering into NewProject Method ...");

                // Check if Customer Exist
                result = SQLFunctionsCustomers.ExistCustomerID(project.CustomerID);
                if (result.RecordsCount != 0)
                {
                    // Check if Project Name Exist
                    result = SQLFunctionsProjects.ExistProjectName(project.ProjectName);
                    if (result.RecordsCount == 0)
                    {
                        // Create new Project
                        using (ScanningDBContext DB = new ScanningDBContext())
                        {
                            Projects New_Record = new Projects();
                            New_Record.ProjectName = project.ProjectName;
                            New_Record.CustomerId  = project.CustomerID;

                            DB.Projects.Add(New_Record);
                            DB.SaveChanges();
                        }
                        result.Message = "NewProject transaction completed successfully. One Record added.";
                    }
                    else
                    {
                        result.ReturnCode = -1;
                        result.Message    = "Project " + project.ProjectName + " already exist. NewProject transaction will be ignore.";
                    }
                }
                else
                {
                    result.ReturnCode = -1;
                    result.Message    = "Customer ID " + project.CustomerID + " does not exist. NewProject transaction will be ignore.";
                }
                logger.Debug(result.Message);
            }
            catch (Exception e)
            {
                logger.Error("Error:" + e.Message + "\n" + "Exception: " + e.InnerException);
                result.ReturnCode = -2;
                result.Message    = e.Message;
                var baseException = e.GetBaseException();
                result.Exception = baseException.ToString();
            }
            logger.Trace("Leaving NewProject Method ...");
            return(result);
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        static public GlobalVars.ResultGeneric UpdateProject(GlobalVars.Project project)
        {
            GlobalVars.ResultGeneric result = new GlobalVars.ResultGeneric()
            {
                ReturnCode     = 0,
                Message        = "",
                RecordsCount   = 0,
                HttpStatusCode = ""
            };
            try
            {
                logger.Trace("Entering into UpdateProject Method ...");

                using (ScanningDBContext DB = new ScanningDBContext())
                {
                    // Project Names must be unique in the Database. The Name could be change but it must be unique
                    Projects Matching_Result = DB.Projects.FirstOrDefault(x => x.ProjectName == project.ProjectName);
                    if (Matching_Result == null)
                    {
                        // Means --> this is a new name
                        Matching_Result = DB.Projects.FirstOrDefault(x => x.ProjectId == project.ProjectID);
                        if (Matching_Result != null)
                        {
                            Matching_Result.ProjectName = project.ProjectName;
                            DB.SaveChanges();
                            result.Message = "UpdateCustomer transaction completed successfully. One Record Updated.";
                        }
                        else
                        {
                            // Means --> cannot update a Customer that does not exist
                            result.ReturnCode = -1;
                            result.Message    = "Project " + project.ProjectName + " does not exist. UpdateProject transaction ignore.";
                        }
                    }
                    else
                    {
                        // Means --> the name already exist
                        result.ReturnCode = -1;
                        result.Message    = "Project " + project.ProjectName + " already exist. UpdateCustomer transaction ignore.";
                    }
                }
                logger.Debug(result.Message);
            }
            catch (Exception e)
            {
                logger.Error("Error:" + e.Message + "\n" + "Exception: " + e.InnerException);
                result.ReturnCode = -2;
                result.Message    = e.Message;
                var baseException = e.GetBaseException();
                result.Exception = baseException.ToString();
            }
            logger.Trace("Leaving UpdateProject Method ...");
            return(result);
        }
        /// <summary>
        /// Get Project by Name
        /// </summary>
        /// <returns></returns>
        static public GlobalVars.ResultProjects GetProjectByName(string projectName)
        {
            List <GlobalVars.Project> projects = new List <GlobalVars.Project>();

            GlobalVars.ResultProjects resultProjects = new GlobalVars.ResultProjects()
            {
                ReturnCode     = 0,
                Message        = "",
                ReturnValue    = projects,
                RecordsCount   = 0,
                HttpStatusCode = ""
            };
            try
            {
                logger.Trace("Entering into GetProjectByName Method ...");
                using (ScanningDBContext DB = new ScanningDBContext())
                {
                    var results = DB.Projects.Where(x => x.ProjectName == projectName);
                    resultProjects.RecordsCount = results.Count();
                    if (results.Count() >= 1)
                    {
                        foreach (var x in results)
                        {
                            GlobalVars.Project project = new GlobalVars.Project()
                            {
                                ProjectID   = x.ProjectId,
                                CustomerID  = x.CustomerId,
                                ProjectName = x.ProjectName
                            };
                            projects.Add(project);
                        }
                    }
                }
                resultProjects.ReturnValue = projects;
                resultProjects.Message     = "GetProjectByName transaction completed successfully. Number of records found: " + resultProjects.RecordsCount;
                logger.Debug(resultProjects.Message);
            }
            catch (Exception e)
            {
                logger.Error("Error:" + e.Message + "\n" + "Exception: " + e.InnerException);
                resultProjects.ReturnCode = -2;
                resultProjects.Message    = e.Message;
                var baseException = e.GetBaseException();
                resultProjects.Exception = baseException.ToString();
            }
            logger.Trace("Leaving GetProjectByName Method ...");
            return(resultProjects);
        }
Ejemplo n.º 4
0
        public ActionResult UpdateProject([FromBody] string projectJS)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            GlobalVars.ResultGeneric result = new GlobalVars.ResultGeneric()
            {
                Message    = "",
                ReturnCode = 0,
                //ReturnValue = ""
            };
            try
            {
                if (projectJS == null)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    result.ReturnCode   = -1;
                    result.Message      = "Missing argument projectJS";
                    logger.Warn("UpdateProject API Request ends with an Error.");
                    logger.Warn(result.Message);
                }
                else
                {
                    GlobalVars.Project project = JsonConvert.DeserializeObject <GlobalVars.Project>(projectJS);
                    logger.Info("UpdateProject API Request.");
                    if (!string.IsNullOrEmpty(project.ProjectName))
                    {
                        logger.Debug("Parameter:" + JsonConvert.SerializeObject(project, Formatting.Indented));

                        result = SQLFunctionsProjects.UpdateProject(project);
                        switch (result.ReturnCode)
                        {
                        case 0:
                            logger.Info("UpdateProject API Request was executed Successfully.");
                            Response.StatusCode = (int)HttpStatusCode.OK;
                            break;

                        case -1:
                            logger.Info("UpdateProject API Request ends with a warning.");
                            Response.StatusCode = (int)HttpStatusCode.OK;
                            break;

                        case -2:
                            Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                            logger.Fatal("UpdateProject API Request ends with a Fatal Error.");
                            logger.Debug("Returned value:" + JsonConvert.SerializeObject(result, Formatting.Indented));
                            Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                            break;
                        }
                    }
                    else
                    {
                        Response.StatusCode = (int)HttpStatusCode.BadRequest;
                        result.ReturnCode   = -1;
                        result.Message      = "UpdateProject value is not valid.";
                        logger.Warn("UpdateProject API Request ends with an Error.");
                        logger.Warn(result.Message);
                    }
                }
            }
            catch (Exception e)
            {
                logger.Fatal("UpdateProject API Request ends with a Fatal Error.");
                result.ReturnCode = -2;
                result.Message    = e.Message;
                var baseException = e.GetBaseException();
                result.Exception = baseException.ToString();
                logger.Fatal("Returned value:" + JsonConvert.SerializeObject(result, Formatting.Indented));
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            }
            Response.ContentType = "application/json";
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            elapsedMs             = elapsedMs / 1000;
            result.ElapsedTime    = elapsedMs.ToString();
            result.HttpStatusCode = Response.StatusCode.ToString();
            var messaje = JsonConvert.SerializeObject(result, Formatting.Indented);

            logger.Info("Leaving UpdateProject API");
            //return Json(messaje);
            return(Content(messaje));
        }