Exemplo n.º 1
0
 public ActionResult Create(SchoolsModel SchoolsObj)
 {
     //------------------------------------------
     //Check ModelState
     //------------------------------------------
     if (!ModelState.IsValid)
     {
         return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Invalid data"));
     }
     //------------------------------------------
     try
     {
         bool result = SchoolsFactor.Create(SchoolsObj);
         if (result == true)
         {
             return(List(1, 25, null, null, null, null));
         }
         else
         {
             return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Saving operation faild"));
         }
     }
     catch (Exception ex)
     { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message)); }
 }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Address,Name")] SchoolsModel schoolsModel)
        {
            if (id != schoolsModel.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(schoolsModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SchoolsModelExists(schoolsModel.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(schoolsModel));
        }
Exemplo n.º 3
0
        //------------------------------------------------------------------------------------------------------
        #endregion

        #region /*--------- Create ---------*\
        //------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Converts the  object properties to SQL paramters and executes the create  procedure 
        /// and updates the object with the SQL data by reference.
        /// </summary>
        /// <param name="SchoolsObj">Model object.</param>
        /// <returns>The result of create query.</returns>
        //--------------------------------------------------------------------
        public bool Create(SchoolsModel SchoolsObj)
        {
            bool result = false;
            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("[dbo].[Schools_Create]", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;
                //----------------------------------------------------------------------------------------------
                // Set the parameters
                //----------------------------------------------------------------------------------------------
				myCommand.Parameters.Add("@SchoolID", SqlDbType.Int).Value = SchoolsObj.SchoolID;
				myCommand.Parameters.Add("@Name", SqlDbType.NVarChar).Value = SchoolsObj.Name;

                //----------------------------------------------------------------------------------------------
                // Execute the command
                //----------------------------------------------------------------------------------------------
                myConnection.Open();
                if (myCommand.ExecuteNonQuery() > 0)
                {
                    result = true;
                    //Get ID value from database and set it in object

                }
                myConnection.Close();
                return result;
                //----------------------------------------------------------------------------------------------
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,Address,Name")] SchoolsModel schoolsModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(schoolsModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(schoolsModel));
        }
Exemplo n.º 5
0
        //------------------------------------------------------------------------------------------------------
        #endregion

        #region --------------GetObject--------------
        //------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Gets the spesific record.
        /// </summary>
        /// <param name="ID">The model id.</param>
        /// <returns>Model object.</returns>
        //--------------------------------------------------------------------
        public static SchoolsModel GetObject(int SchoolID)
        {
            SchoolsModel        SchoolsObj = null;
            List <SchoolsModel> list       = SchoolsSqlDataPrvider.Instance.Get(SchoolID);

            if (list != null && list.Count > 0)
            {
                SchoolsObj = list[0];
            }
            return(SchoolsObj);
        }
Exemplo n.º 6
0
 public ActionResult GetObject(int id)
 {
     try
     {
         SchoolsModel SchoolsObj = SchoolsFactor.GetObject(id);
         if (SchoolsObj == null)
         {
             SchoolsObj = new SchoolsModel();
         }
         return(Json(SchoolsObj, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
Exemplo n.º 7
0
        //------------------------------------------------------------------------------------------------------
        #endregion

        #region /*--------- PopulateModelFromIDataReader ---------*\
        //------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Populates model from IDataReader .
        /// </summary>
        /// <param name="reader"></param>
        /// <returns>Model object.</returns>
        //--------------------------------------------------------------------
        private SchoolsModel PopulateModelFromIDataReader(IDataReader reader)
        {
            //Create a new object
            SchoolsModel SchoolsObj = new SchoolsModel();
            //Fill the object with data

			//------------------------------------------------
			//[SchoolID]
			//------------------------------------------------
			if (reader["SchoolID"] != DBNull.Value)
			    SchoolsObj.SchoolID = (int)reader["SchoolID"];
			//------------------------------------------------
			//------------------------------------------------
			//[Name]
			//------------------------------------------------
			if (reader["Name"] != DBNull.Value)
			    SchoolsObj.Name = (string)reader["Name"];
			//------------------------------------------------
            //Return the populated object
            return SchoolsObj;
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            if (ModelState.IsValid)
            {
                if (fileExcel != null)
                {
                    using (var stream = new FileStream(fileExcel.FileName, FileMode.Create))
                    {
                        await fileExcel.CopyToAsync(stream);

                        using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled))
                        {
                            foreach (IXLWorksheet worksheet in workBook.Worksheets)
                            {
                                SchoolsModel newSchool;

                                var c = _context.Schools.Where(c => c.Name == worksheet.Name).ToList();
                                if (c.Count > 0)
                                {
                                    newSchool = c[0];
                                }
                                else
                                {
                                    newSchool      = new SchoolsModel();
                                    newSchool.Name = worksheet.Name;
                                    _context.Schools.Add(newSchool);
                                }
                                await _context.SaveChangesAsync();

                                foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                {
                                    try
                                    {
                                        StudentsModel student = new StudentsModel();
                                        student.Name        = row.Cell(1).Value.ToString();
                                        student.DateOfBirth = row.Cell(2).GetDateTime();
                                        ClassesModel cl;
                                        var          classes = _context.Classes.Where(c => c.Name.Contains(row.Cell(3).Value.ToString()) && c.School == newSchool).FirstOrDefault();
                                        if (classes == null)
                                        {
                                            cl        = new ClassesModel();
                                            cl.Name   = row.Cell(3).Value.ToString();
                                            cl.School = newSchool;
                                            cl.Info   = "Import with EXCEL!";
                                            _context.Classes.Add(cl);
                                            classes = cl;
                                        }
                                        student.Class = classes;
                                        var students = _context.Students.Where(s => s.Name == student.Name && s.DateOfBirth == student.DateOfBirth && s.Class == classes).FirstOrDefault();
                                        if (students == null)
                                        {
                                            _context.Students.Add(student);
                                        }
                                        await _context.SaveChangesAsync();
                                    }
                                    catch (Exception e)
                                    {
                                    }
                                }
                            }
                        }
                    }
                }

                await _context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 9
0
        //------------------------------------------------------------------------------------------------------
        #endregion

        #region --------------Update--------------
        //------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Updates model object by calling sql data provider update method.
        /// </summary>
        /// <param name="SchoolsObj">The model object.</param>
        /// <returns>The result of update operation.</returns>
        //--------------------------------------------------------------------
        public static bool Update(SchoolsModel SchoolsObj)
        {
            return(SchoolsSqlDataPrvider.Instance.Update(SchoolsObj));
        }