Exemplo n.º 1
0
        public async Task <IActionResult> UploadGroupImage(IFormFile file, [FromRoute] int groupId)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!await _groupService.ExistsAsync(groupId))
            {
                return(BadRequest("Group not found"));
            }
            if (!User.IsInRole("Admin"))
            {
                Student studentUser = await _studentService.GetByIdAsync(Int32.Parse(User.Identity.Name));

                if (studentUser.Group != null)
                {
                    if (studentUser.Group.Id != groupId)
                    {
                        return(BadRequest("You are not in this group"));
                    }
                    else if (!studentUser.GroupAdmin)
                    {
                        return(BadRequest("You are not this group's admin"));
                    }
                }
                else
                {
                    return(BadRequest("You are not in any group"));
                }
            }
            try
            {
                string imgName = await _imageService.UploadImage(file);

                Group result = await _groupService.GetByIdAsync(groupId);

                if (result.Picture != null)
                {
                    _imageService.DeleteImage(result.Picture);
                }

                await _groupService.UploadGroupImageAsync(result, imgName, Int32.Parse(User.Identity.Name));

                return(Ok(imgName));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }