public async Task <ActionResult <Guid> > Post([FromForm]  IFormFile file)
      {
          if (file.Length == 0)
          {
              return(NoContent());
          }

          var media = new Media();

          var blockBlob = _container.GetBlockBlobReference(file.FileName);

          if (file.Length > 0)
          {
              using (var fileStream = file.OpenReadStream())
              {
                  await blockBlob.UploadFromStreamAsync(fileStream);
              }

              media.Name = blockBlob.Name;
              media.Type = resolveMediaType(file.ContentType);
              media.Url  = blockBlob.Uri.ToString();
          }


          await _ctx.Media.AddAsync(media);

          await _ctx.SaveChangesAsync();

          return(Ok(new { Id = media.Id }));
      }
        public async Task <ActionResult> AddPersonToTree(Guid id, [FromBody] PersonCreationDto person)
        {
            var tree = await _ctx.Trees.FindAsync(id);

            if (tree == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var mappedPerson = _mapper.Map <Person>(person);

            mappedPerson.Tree = tree;
            await _ctx.Persons.AddAsync(mappedPerson);

            await _ctx.SaveChangesAsync();

            var creationResult = _mapper.Map <PersonDto>(mappedPerson);

            return(Ok(new { Id = creationResult.Id }));
        }
Пример #3
0
        public async Task <ActionResult> AddAvatar([FromBody] AddAvatarToUserDto request)
        {
            var user  = _userService.GetById(request.UserId);
            var media = await _ctx.Media.FindAsync(request.MediaId);

            if (user == null || media == null)
            {
                return(NotFound());
            }

            if (media.Type != MediaType.Picture)
            {
                return(BadRequest());
            }
            var currentAvatar = await _ctx.Media.Include(d => d.User)
                                .Where(d => (d.User.Id == user.AvatarId) && (d.Type == MediaType.Avatar))
                                .SingleOrDefaultAsync();

            if (currentAvatar != null)
            {
                currentAvatar.Type = MediaType.Picture;
                _ctx.Update(currentAvatar);
            }
            media.User = user;
            media.Type = MediaType.Avatar;

            _ctx.Media.Update(media);
            await _ctx.SaveChangesAsync();

            return(Ok(new { mediaId = media.Id }));
        }
Пример #4
0
        public static async Task ResetAsync(this TreeDbContext context)
        {
            await context.Database.EnsureDeletedAsync();

            await context.Database.EnsureCreatedAsync();

            var root = new Node
            {
                Name     = "Root",
                Children =
                {
                    new Node
                    {
                        Name = "Node 1"
                    },
                    new Node
                    {
                        Name     = "Node 2",
                        Children =
                        {
                            new Node
                            {
                                Name     = "Node 2.1",
                                Children =
                                {
                                    new Node {
                                        Name = "Node 2.1.1"
                                    },
                                    new Node {
                                        Name = "Node 2.1.2"
                                    }
                                }
                            },
                            new Node         {
                                Name = "Node 2.2"
                            }
                        }
                    }
                }
            };

            await context.Nodes.AddAsync(root);

            await context.SaveChangesAsync();
        }