public async Task <IActionResult> DeleteProduct(int userId, int id)
        {
            var productFromRepo = await _repo.GetProduct(id);

            var mediaFromRepo = productFromRepo.Media;

            foreach (Media m in mediaFromRepo)
            {
                if (m.Type == "image" || m.Type == "file")
                {
                    if (m.Url != null)
                    {
                        var fileName = m.Url;
                        var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images", fileName);

                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                    }
                }
            }

            _repo.Delete(productFromRepo);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Failed to delete the product"));
        }
예제 #2
0
        public async Task <IActionResult> DeleteOrganizationType(int id, int userId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var otFromRepo = await _repo.GetOrganizationType(id);

            if (otFromRepo.OrganizationAndType.Count > 0)
            {
                return(BadRequest("organization type has organizations"));
            }
            _repo.Delete(otFromRepo);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Failed to delete the organization type"));
        }
예제 #3
0
        public async Task <IActionResult> DeleteCourse(int id, int userId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var courseFromRepo = await _repo.GetCourse(id);

            var courseToCheck = _mapper.Map <CourseDto>(courseFromRepo);

            if (courseToCheck.Counter != 0)
            {
                return(BadRequest("course has products"));
            }
            _repo.Delete(courseFromRepo);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Failed to delete the course"));
        }
예제 #4
0
        public async Task <IActionResult> DeleteOrganization(int id, int userId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var organizationFromRepo = await _repo.GetOrganization(id);

            var organizationToCheck = _mapper.Map <OrganizationDto>(organizationFromRepo);

            if (organizationToCheck.Counter != 0)
            {
                return(BadRequest("student has products"));
            }
            _repo.Delete(organizationFromRepo);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Failed to delete the organization"));
        }
예제 #5
0
        public async Task <IActionResult> DeleteMedia(int productId, int id)
        {
            var product = await _repo.GetProduct(productId);

            if (!product.Media.Any(m => m.Id == id))
            {
                return(Unauthorized());
            }

            var mediaFromRepo = await _repo.GetMedia(id);

            if (mediaFromRepo.IsMain)
            {
                return(BadRequest("You can not delete your main photo"));
            }

            if (mediaFromRepo.Url != null)
            {
                if (mediaFromRepo.Type != "video" && mediaFromRepo.Type != "link")
                {
                    var fileName = mediaFromRepo.Url;
                    var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images", fileName);

                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }
                }
            }

            _repo.Delete(mediaFromRepo);
            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Failed to delete the media"));
        }