public async Task <int> SaveMetadata(MetadataModel model)
        {
            var metaEntity = new Metadata
            {
                Caption    = model.Title,
                LastChange = DateTime.Now,
                User       = await this.usersService.GetCurrentApplicationUser(),
                Content    = model.Content
            };

            if (model.File != null)
            {
                var file = new MetaFile
                {
                    Name     = model.File.Filename,
                    MimeType = model.File.MimeType,
                    Content  = Convert.FromBase64String(model.File.Base64Content)
                };
                metaEntity.MetaFile.Add(file);
            }

            if (model.TranslatedFile != null)
            {
                var file = new MetaTranslatedFile
                {
                    Name     = model.TranslatedFile.Filename,
                    MimeType = model.TranslatedFile.MimeType,
                    Content  = Convert.FromBase64String(model.TranslatedFile.Base64Content)
                };
                metaEntity.MetaTranslatedFile.Add(file);
            }

            var metaInserted = this.context.Metadata.Add(metaEntity);

            this.SaveAutoCompleteSuggestionsAdapted(model.Content);


            await this.context.SaveChangesAsync();

            return(metaInserted.Entity.Id);
        }
        public async Task EditMetadata(MetadataModel model, int id)
        {
            var metaInfo = await this.context.Metadata.Where(x => x.Id == id).Select(x => new
            {
                Meta         = x,
                OrgShortName = x.User.Organization.ShortName,
                x.UserId
            }).SingleAsync();

            var hasRightToEdit = this.HasEditDeleteRights(metaInfo.UserId, metaInfo.OrgShortName);

            if (!hasRightToEdit)
            {
                throw new NotAuthorizedException();
            }

            var oldRecord = metaInfo.Meta;

            if (oldRecord == null)
            {
                throw new NotFoundException($"Metadata with id {id} not found");
            }

            oldRecord.Content    = model.Content;
            oldRecord.LastChange = DateTime.Now;
            oldRecord.Caption    = model.Title;
            if (model.File != null && model.File.Base64Content != null)
            {
                var file = new MetaFile
                {
                    Name     = model.File.Filename,
                    MimeType = model.File.MimeType,
                    Content  = Convert.FromBase64String(model.File.Base64Content)
                };
                var oldFileId = this.context.MetaFile.Where(x => x.MetadataId == id).Select(x => x.Id).FirstOrDefault();
                if (oldFileId == 0)
                {
                    oldRecord.MetaFile.Add(file);
                }
                else
                {
                    file.Id         = oldFileId;
                    file.MetadataId = id;
                    var entry = this.context.MetaFile.Attach(file);
                    entry.State = EntityState.Modified;
                }
            }
            else if (model.File == null)
            {
                var oldFileId = this.context.MetaFile.Where(x => x.MetadataId == id).Select(x => x.Id).FirstOrDefault();
                if (oldFileId != 0)
                {
                    var entry = this.context.MetaFile.Attach(new MetaFile {
                        Id = oldFileId
                    });
                    entry.State = EntityState.Deleted;
                }
            }

            if (model.TranslatedFile != null && model.TranslatedFile.Base64Content != null)
            {
                var file = new MetaTranslatedFile
                {
                    Name     = model.TranslatedFile.Filename,
                    MimeType = model.TranslatedFile.MimeType,
                    Content  = Convert.FromBase64String(model.TranslatedFile.Base64Content)
                };
                var oldFileId = this.context.MetaTranslatedFile.Where(x => x.MetadataId == id).Select(x => x.Id)
                                .FirstOrDefault();
                if (oldFileId == 0)
                {
                    oldRecord.MetaTranslatedFile.Add(file);
                }
                else
                {
                    file.Id         = oldFileId;
                    file.MetadataId = id;
                    var entry = this.context.MetaTranslatedFile.Attach(file);
                    entry.State = EntityState.Modified;
                }
            }
            else if (model.TranslatedFile == null)
            {
                var oldFileId = this.context.MetaTranslatedFile.Where(x => x.MetadataId == id).Select(x => x.Id)
                                .FirstOrDefault();
                if (oldFileId != 0)
                {
                    var entry = this.context.MetaTranslatedFile.Attach(new MetaTranslatedFile {
                        Id = oldFileId
                    });
                    entry.State = EntityState.Deleted;
                }
            }

            var log = this.mapper.Map <MetadataLog>(oldRecord);

            log.UserId = this.currentUserService.GetCurrentUserIdClaim();
            this.context.MetadataLog.Add(log);
            this.SaveAutoCompleteSuggestionsAdapted(model.Content);
            await this.context.SaveChangesAsync();
        }