Ejemplo n.º 1
0
        public async Task <ActionResult <TheoryCourse> > PostTheoryCourse([FromForm] TheoryCourse theoryCourse, IFormFile image)
        {
            var identity = User.Identity as System.Security.Claims.ClaimsIdentity;
            IEnumerable <System.Security.Claims.Claim> claims = identity.Claims;
            var national_code = claims.Where(p => p.Type == "national_code").FirstOrDefault()?.Value;

            var staff = _context.staffs.Where(q => q.national_code == national_code).FirstOrDefault();

            if (staff == null)
            {
                return(Unauthorized());
            }

            theoryCourse.staff_id = staff.id;

            _context.theory_courses.Add(theoryCourse);

            await _context.SaveChangesAsync();

            var image_name = ImagesHelper.save(image);

            _context.images.Add(new Image
            {
                name           = image_name,
                imageable_id   = theoryCourse.id,
                imageable_type = "TheoryCourse"
            });

            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetTheoryCourse", new { id = theoryCourse.id }, theoryCourse));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <TheoryCourse> > PutTheoryCourse(int id, [FromForm] TheoryCourse theoryCourse, IFormFile image)
        {
            theoryCourse.updated_at            = DateTime.Now;
            _context.Entry(theoryCourse).State = EntityState.Modified;

            if (image != null)
            {
                string image_name;

                var pImage = _context.images.Where(i => (i.imageable_id == theoryCourse.id && i.imageable_type == "TheoryCourse")).FirstOrDefault();

                ImagesHelper.delete(pImage.name);

                image_name = ImagesHelper.save(image);

                var local = _context.Set <Image>().Local.FirstOrDefault(i => i.id == pImage.id);

                if (local != null)
                {
                    _context.Entry(local).State = EntityState.Detached;
                }

                _context.Entry(new Image
                {
                    id             = pImage.id,
                    name           = image_name,
                    imageable_id   = theoryCourse.id,
                    imageable_type = "TheoryCourse"
                }).State = EntityState.Modified;
            }


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

            return(NoContent());
        }