//Datei aus DB ins Memory laden um es anzuzeigen
        public FileStreamResult GetFile(int id)
        {
            File   file       = db.File.Where(f => f.Id == id).SingleOrDefault();
            Stream fileStream = new MemoryStream(file.File1);

            return(new FileStreamResult(fileStream, file.Ending));
        }
        public async Task <IActionResult> CreateConcernAsync(Concern concern, List <IFormFile> files)
        {
            if (ModelState.IsValid)
            {
                // Image[] files = concern.Image.ToArray();
                concern.UserId = (await userManager.GetUserAsync(HttpContext.User)).Id;
                concern.Date   = DateTime.UtcNow;

                long size = files.Sum(f => f.Length);

                // full path to file in temp location
                var filePath = Path.GetTempFileName();
                foreach (var formFile in files)
                {
                    File  file  = new File();
                    Image image = new Image();
                    if (formFile.Length > 0)
                    {
                        using (var stream = new MemoryStream())
                        {
                            await formFile.CopyToAsync(stream);

                            if (string.Equals(formFile.ContentType, "application/pdf",
                                              StringComparison.OrdinalIgnoreCase)) /*"\"application/pdf\"")*/
                            {
                                file.File1     = stream.ToArray();
                                file.ConcernId = concern.Id;
                                file.PollId    = null;
                                file.Name      = formFile.FileName;
                                file.Ending    = formFile.ContentType;
                                concern.File.Add(file);
                            }
                            else
                            {
                                image.Img       = stream.ToArray();
                                image.ConcernId = concern.Id;
                                image.PollId    = null;
                                image.Name      = formFile.FileName;
                                image.Ending    = formFile.ContentType;
                                concern.Image.Add(image);
                            }
                        }
                    }
                }


                concern.StatusId = 1;
                db.Add(concern);
                await db.SaveChangesAsync();

                return(RedirectToAction("ShowConcern", "Home", new { concernId = concern.Id }));
            }

            return(View("CreateConcernView"));
        }
        public async Task <IActionResult> CreatePollAsync([FromBody] CreatePollModel createPoll)
        {
            if (ModelState.IsValid)
            {
                //Variablen
                int        concernStatusId = 0;
                int        userId          = (await userManager.GetUserAsync(HttpContext.User)).Id;
                List <int> answerOptionId  = new List <int>();
                Poll       poll            = new Poll
                {
                    Text              = createPoll.Text,
                    Title             = createPoll.Title,
                    End               = createPoll.End,
                    NeedsLocalCouncil = createPoll.NeedsLocalCouncil,
                    LastUpdatedBy     = userId,
                    LastUpdatedAt     = DateTime.UtcNow,
                    UserId            = userId,
                    StatusId          = 2,
                    CategoryId        = createPoll.CategoryId
                };
                //Nur, wenn Umfrage aus Anliegen erstellt wird, wird das Anliegen auf Status "abgeschlossen" gesetzt.
                if (createPoll.ConcernId != 0)
                {
                    Concern concern = db.Concern.Where(c => c.Id == createPoll.ConcernId).SingleOrDefault();
                    concernStatusId  = 6; //concern.StatusId;
                    concern.StatusId = concernStatusId;
                    db.Update(concern);
                }

                //Antworten aus Objekt auslesen und in Datenbank speichern
                foreach (string answer in createPoll.Answers)
                {
                    AnswerOptions answerOption = null;
                    //Prüfung, ob Antwort schon einmal verwendet wurde
                    answerOption = db.AnswerOptions.Where(ao => ao.Description.Equals(answer)).SingleOrDefault();
                    if (answerOption != null)
                    {
                        answerOptionId.Add(answerOption.Id);
                    }
                    else
                    {
                        answerOption = new AnswerOptions {
                            Description = answer
                        };
                        db.Add(answerOption);
                        answerOptionId.Add(answerOption.Id);
                    }
                }

                if (!poll.NeedsLocalCouncil)
                {
                    poll.Approved = true;
                }
                else
                {
                    poll.Approved = false;
                }
                //Umfrage Speichern
                db.Add(poll);

                //Antworten Speichern
                foreach (int aoId in answerOptionId)
                {
                    db.Add(new AnswerOptionsPoll {
                        PollId = poll.Id, AnswerOptionsId = aoId
                    });
                }

                //Dokumente aus dem Anliegen in die Umfrage übernehmen
                if (createPoll.FileIds != null)
                {
                    foreach (int fileId in createPoll.FileIds)
                    {
                        File file = db.File.Where(f => f.Id == fileId).SingleOrDefault();
                        file.PollId = poll.Id;
                        db.Update(file);
                    }
                }
                //Bilder aus dem Anliegen in die Umfrage übernehmen
                if (createPoll.ImageIds != null)
                {
                    foreach (int imageId in createPoll.ImageIds)
                    {
                        Image image = db.Image.Where(i => i.Id == imageId).SingleOrDefault();
                        image.PollId = poll.Id;
                        db.Update(image);
                    }
                }
                //Commit
                int result = db.SaveChanges();
                return(Json(new { result, concernStatusId, concernId = createPoll.ConcernId }));
            }

            return(Json(new { result = 0 }));
        }