private void AddImageDataToDb(DalPicture picture)
        {
            using (SqlConnection conn = new SqlConnection(_connectionString))
                using (SqlCommand command = new SqlCommand("AddPicture", conn))
                {
                    command.CommandType = CommandType.StoredProcedure;

                    addParameter(command, "Id", picture.Id);
                    addParameter(command, "UserId", picture.UserId);
                    addParameter(command, "Name", picture.Name);
                    addParameter(command, "Date", picture.Date);
                    addParameter(command, "@FileExtension", picture.FileExtension);

                    conn.Open();
                    command.ExecuteNonQuery();
                    conn.Close();
                }

            // Add Metatag to db and add metatag link in link table
            foreach (string metaTag in picture.MetaTags)
            {
                // Add metatag and get metatag id
                string id = AddMetaTag(metaTag);

                // Add link between picture and metatag
                addPictureMetaTagLink(id, picture.Id);
            }
        }
        public async Task <List <string> > UploadToBlobAsync([FromBody] List <LogicPicture> pictures)
        {
            if (pictures.Count > 0)
            {
                // Get user ID
                string token = Request.Headers.GetCommaSeparatedValues("Authorization").First().Remove(0, 7);

                string email = new JwtSecurityTokenHandler().ReadJwtToken(token).Subject;

                IdentityUser user = new IdentityUser
                {
                    Email    = email,
                    UserName = email
                };

                user = await _userManager.GetUserAsync(HttpContext.User);

                // Convert LogicPicture to DalPicture
                List <DalPicture> dalPictures = new List <DalPicture>();
                foreach (LogicPicture picture in pictures)
                {
                    string fileExtension = Regex.Match(picture.Base64, @"(?<=\/)(.*)(?=;)").Groups.FirstOrDefault().Value;
                    string id            = Convert.ToString(Guid.NewGuid());
                    byte[] base64        = Convert.FromBase64String(picture.Base64.Split(',')[1]);

                    DalPicture dalPicture = new DalPicture()
                    {
                        Name          = picture.Name,
                        UserId        = user.Id,
                        Id            = id,
                        Base64        = base64,
                        MetaTags      = picture.MetaTags,
                        Date          = picture.Date,
                        FileExtension = fileExtension,
                    };

                    dalPictures.Add(dalPicture);
                }

                return(await _dalPicture.StorePictureInBlobStorage(dalPictures));
            }

            return(null);
        }
        public List <DalPicture> GetPictures(int amount = 0, int offset = 0)
        {
            List <DalPicture> pictureList = new List <DalPicture>();

            // Open connection and execute procedure
            using (SqlConnection conn = new SqlConnection(_connectionString))
                using (SqlCommand command = new SqlCommand("GetPictures", conn))
                {
                    command.CommandType = CommandType.StoredProcedure;

                    // Check for negative values and exclude them if so
                    if (amount > 0)
                    {
                        addParameter(command, "amount", amount.ToString());
                    }
                    if (offset > 0)
                    {
                        addParameter(command, "offset", offset.ToString());
                    }

                    conn.Open();
                    DbDataReader reader = command.ExecuteReader();

                    // Loop through all row
                    while (reader.Read())
                    {
                        // Create picture element
                        DalPicture newPicture = new DalPicture
                        {
                            Id            = reader.GetValue(0).ToString(),
                            UserId        = reader.GetValue(1).ToString(),
                            FileExtension = reader.GetValue(2).ToString()
                        };

                        // Add to list
                        pictureList.Add(newPicture);
                    }

                    conn.Close();
                }

            return(pictureList);
        }