public ActionResult CreateNewSnippet(CreateSnippetModel inModel)
        {
            UserDTO user = GetCurrentUserData();
            var model = new CreateSnippetModel(_displayLanguages);

            if (ModelState.IsValid)
            {
                try
                {
                    var newSnippet = new SnippetDTO
                                         {
                                             Guid = Guid.NewGuid(),
                                             Name = inModel.Name,
                                             Description = inModel.Description,
                                             Data = Encoding.UTF8.GetBytes(inModel.Data),
                                             IsPublic = true,
                                             Language_Id = inModel.LanguageId,
                                             LastModified = DateTime.UtcNow,
                                             User_Id = user.Id,
                                             User_FormsAuthId = user.FormsAuthId
                                         };

                    var files = new List<FileDTO>();
                    if (CurrentSnippet.UploadModel.Files.Any())
                    {
                        for (int i = 0; i < CurrentSnippet.UploadModel.Files.Count(); i++)
                        {
                            NamedFileWithDescription file =
                                ((List<NamedFileWithDescription>) CurrentSnippet.UploadModel.Files)[i];
                            var temp = new FileDTO
                                           {
                                               Name = file.Name,
                                               Description = file.Description,
                                               Data = file.File.Data,
                                               LastModified = DateTime.UtcNow
                                           };
                            files.Add(temp);
                        }
                    }

                    // The new Guid and snippet id should be returned to us
                    newSnippet = _managerService.CreateSnippet(
                        newSnippet,
                        new LanguageDTO {Id = inModel.LanguageId},
                        user,
                        files.ToArray(),
                        CurrentSnippet.Hyperlinks.ToArray());

                    var detailsModel = new SnippetDetailsModel
                                           {
                                               UserId = user.Id,
                                               UserGuid = user.FormsAuthId,
                                               UserName = user.LoginName,
                                               UserAvatar = user.AvatarImage,
                                               PreviewData = Encoding.UTF8.GetString(newSnippet.PreviewData),
                                               Data = Encoding.UTF8.GetString(newSnippet.Data),
                                               Name = newSnippet.Name,
                                               Description = newSnippet.Description,
                                               IsPublic = inModel.IsPublic,
                                               SnippetId = newSnippet.Guid,
                                               SnippetUrl = newSnippet.Guid.ToString(),
                                               Languages = _displayLanguages,
                                               Files =
                                                   CurrentSnippet.UploadModel.Files ??
                                                   new List<NamedFileWithDescription>(),
                                               Hyperlinks =
                                                   CurrentSnippet.Hyperlinks ?? new List<HyperlinkDTO>(),
                                               LanguageId = inModel.LanguageId
                                           };
                    UploadedFiles = null;
                    CurrentSnippet.Hyperlinks = null;
                    TempData["Message"] = "Saved Snippet Id: " + newSnippet.Guid + " Successfully";

                    return PartialView("_SnippetDetailsTablePartial", detailsModel);
                }
                catch (Exception ex)
                {
                    Logger.LogError("CreateNewSnippet action method failed", ex);
                    if (inModel.Languages == null) inModel.Languages = _displayLanguages;
                    if (inModel.Hyperlinks == null) inModel.Hyperlinks = CurrentSnippet.Hyperlinks;
                    if (inModel.UploadModel == null) inModel.UploadModel = CurrentSnippet.UploadModel;
                    TempData["Error"] = "Unknown error occurred.";
                    return View(inModel);
                }
            }
            else
            {
                inModel.Languages = _displayLanguages;
                TempData["Error"] = "Please fix any errors to continue";
                return View(inModel);
            }
        }
        public ActionResult SingleSnippet(string guid)
        {
            var viewModel = new SnippetDetailsModel();
            var userId = -1;
            Guid userGuid;
            try
            {
                SnippetDTO snippet = _managerService.GetSnippet(guid);
                userId = snippet.User_Id;
                userGuid = snippet.User_FormsAuthId;

                if (snippet != null)
                {
                    UserDTO user = _managerService.UserDetails(snippet.User_Id, snippet.Guid);
                    if (user == null)
                    {
                        TempData["Error"] = "Error getting user information for snippet";
                        return View(viewModel);
                    }

                    viewModel = new SnippetDetailsModel
                                    {
                                        Data = Encoding.UTF8.GetString(snippet.Data),
                                        PreviewData = Encoding.UTF8.GetString(snippet.PreviewData),
                                        SnippetId = snippet.Guid,
                                        Name = snippet.Name,
                                        Description = snippet.Description,
                                        SnippetUrl = snippet.Guid.ToString(),
                                        UserId = snippet.User_Id,
                                        UserGuid = user.FormsAuthId,
                                        UserName = user.LoginName,
                                        UserAvatar = user.AvatarImage,
                                        Hyperlinks = new List<HyperlinkDTO>(),
                                        Files = new List<NamedFileWithDescription>()
                                    };
                    HyperlinkDTO[] hyperlinks = _managerService.GetHyperlinksForSnippet(snippet.Guid, user.Id,
                                                                                        user.FormsAuthId);
                    if (hyperlinks != null && hyperlinks.Any())
                        viewModel.Hyperlinks = hyperlinks;

                    FileDTO[] files = _managerService.GetFilesForSnippet(snippet.Guid, user.Id, user.FormsAuthId);
                    if (files != null && files.Any())
                    {
                        foreach (NamedFileWithDescription file in files.Select(fileDTO => new NamedFileWithDescription
                                                                                              {
                                                                                                  File = fileDTO,
                                                                                                  Name = fileDTO.Name,
                                                                                                  Description =
                                                                                                      fileDTO.
                                                                                                      Description
                                                                                              }))
                        {
                            ((List<NamedFileWithDescription>) viewModel.Files).Add(file);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("Snippet exception", ex);
                TempData["Error"] = "Unknow error occurred.";
                return RedirectToAction("All");
            }

            // If the user has opted to receive notifications, add one now
            var newMessage = new NotificationDTO
                                 {
                                     User_Id = userId,
                                     User_FormsAuthId = userGuid,
                                     NotificationType_Id = 1,
                                     Text = "Your snipppet (Id: " + guid + ") has been viewed.",
                                     DateCreated = DateTime.UtcNow
                                 };
            _managerService.CreateNewUserNotification(newMessage);

            return View(viewModel);
        }
        public ActionResult SingleSnippet(string guid)
        {
            var viewModel = new SnippetDetailsModel();
            SnippetDTO snippet = _managerService.GetSnippet(guid);
            try
            {
                if (snippet != null)
                {
                    UserDTO user = _managerService.UserDetails(snippet.User_Id, snippet.User_FormsAuthId);
                    if (user == null)
                        throw new SecurityException("Unable to retrieve user information for this private snippet.");

                    viewModel = new SnippetDetailsModel
                                    {
                                        Name = snippet.Name,
                                        Description = snippet.Description,
                                        SnippetUrl = snippet.Guid.ToString(),
                                        PreviewData =
                                            Encoding.UTF8.GetString(snippet.PreviewData),
                                        UserName =
                                            (user != null && snippet.Id > 0)
                                                ? user.LoginName
                                                : "Anonymous",
                                        UserAvatar =
                                            (user != null && snippet.Id > 0)
                                                ? user.AvatarImage
                                                : null,
                                        UserId = snippet.Id,
                                        UserGuid = user.FormsAuthId,
                                        IsPublic = snippet.IsPublic
                                    };
                }
            }
            catch (SecurityException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("IndexError", ex);
                ViewBag.Message = ex.Message;
                return View(viewModel);
            }

            return View(viewModel);
        }