public async Task <IActionResult> PutFields(int id, Fields fields)
        {
            if (id != fields.FieldId)
            {
                return(BadRequest());
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FieldsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutStudents(int id, Students students)
        {
            if (id != students.StudentId)
            {
                return(BadRequest());
            }

            var result = await _context.Students.FindAsync(id);

            result.Name        = students.Name;
            result.Birthdate   = students.Birthdate;
            result.PhoneNumber = students.PhoneNumber;
            result.Address     = students.Address;
            result.Email       = students.Email;
            result.Password    = EncryptPassword(students.Password);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StudentsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #3
0
        public async Task <IActionResult> PutCourseScores(int id, CourseScores courseScores)
        {
            if (id != courseScores.CourseScoreId)
            {
                return(BadRequest());
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CourseScoresExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Upload(List <IFormFile> files, [FromForm] int?CourseId, [FromForm] string Description, [FromForm] string Author)
        {
            if (files == null || files.Count == 0)
            {
                return(BadRequest("Files not selected"));
            }

            if (CourseId == null)
            {
                return(BadRequest("Course id is empty"));
            }

            List <Files> listOfFiles = new List <Files>();

            foreach (var formFile in files)
            {
                var uniqueName = GetUniqueFileName(formFile.FileName);

                var filePath = Path.Combine(he.WebRootPath, "files", uniqueName);

                var size = formFile.Length;

                Files file = new Files
                {
                    CourseId    = CourseId,
                    Name        = uniqueName,
                    Description = Description,
                    Author      = Author,
                    Type        = GetContentType(filePath),
                    Size        = size,
                    Status      = true,
                };

                if (formFile.Length > 0)
                {
                    using var stream = new FileStream(filePath, FileMode.Create);
                    await formFile.CopyToAsync(stream);
                }

                file.FilePath = Encoding.ASCII.GetBytes(filePath);

                listOfFiles.Add(file);

                _context.Files.Add(file);
            }
            await _context.SaveChangesAsync();

            return(Ok(listOfFiles));
        }