Exemplo n.º 1
0
        public IActionResult SavePicture(string sourceUrl, string fileName, int height, int width)
        {
            try
            {
                var picture = new ObjView.Picture
                {
                    SourceUrl = sourceUrl,
                    FileName  = fileName,
                    Height    = height,
                    Width     = width
                };
                // Get all the categories previously saved by the user
                var categories     = _categoryService.GetCategoriesByUser(GetUserEmail());
                var viewCategories = new List <ObjView.Category>();

                if (categories != null)
                {
                    // Convert DB object to View object
                    foreach (var item in categories)
                    {
                        viewCategories.Add(ObjectTransformations.TransformDbtoViewObj(item));
                    }
                }
                var model = new PictureCategoryViewModel(picture, viewCategories);
                return(View(model));
            }
            catch (System.Exception)
            {
                return(RedirectToAction("Index", "SearchGifsController"));
            }
        }
Exemplo n.º 2
0
 public IActionResult Save(ObjView.Picture picture)
 {
     try
     {
         if (picture != null)
         {
             var downloadedData = downloadFileFromUrl(picture.SourceUrl);
             if (downloadedData != null)
             {
                 if (picture.CategoryId < 1)
                 {
                     // Add the "GENERAL category to every user only the first time (when there's no category available)
                     picture.CategoryId = _categoryService.AddEditCategory(new ObjDb.Category
                     {
                         CategoryId  = picture.CategoryId,
                         Name        = Constants.DEFAULT_PIC_CATEGORY,
                         Description = Constants.DEFAULT_PIC_CATEGORY,
                         UserEmail   = GetUserEmail()
                     });
                 }
                 // Create the DB Picture object
                 var newDbPic = new ObjDb.Picture
                 {
                     UserEmail   = GetUserEmail(),
                     CategoryId  = picture.CategoryId,
                     Name        = picture.Name,
                     Description = picture.Description,
                     FileName    = picture.FileName,
                     SourceUrl   = picture.SourceUrl,
                     Height      = picture.Height,
                     Width       = picture.Width,
                     Image       = downloadedData
                 };
                 // Save the img in the database
                 _pictureService.AddGifAnimatedToDB(newDbPic);
                 ViewBag.Message = "The Picture Has Been Saved Successfully in your Profile!";
             }
         }
     }
     catch (System.Exception)
     {
         ViewBag.Message = "Unexpected error when saving the Picture in the Database. Try again later!";
     }
     return(View());
 }
Exemplo n.º 3
0
 /// <summary>
 /// Transform a picture database object to a Category view Obj
 /// </summary>
 /// <param name="picture"></param>
 /// <returns></returns>
 public static ViewObj.Picture TransformDbtoViewObj(DbObj.Picture picture)
 {
     ViewObj.Picture newPicture = null;
     if (picture != null)
     {
         newPicture = new ViewObj.Picture
         {
             PictureId   = picture.PictureId,
             CategoryId  = picture.CategoryId,
             Name        = picture.Name,
             Description = picture.Description,
             FileName    = picture.FileName,
             SourceUrl   = picture.SourceUrl,
             Height      = picture.Height,
             Width       = picture.Width
         };
     }
     return(newPicture);
 }
Exemplo n.º 4
0
 public IActionResult Edit(ObjView.Picture picture)
 {
     try
     {
         if (picture != null && ModelState.IsValid)
         {
             var newDbPic = ObjectTransformations.TransformViewToDbObj(picture);
             // Save the img in the database
             _pictureService.UpdateGifAnimatedToDB(newDbPic);
             ViewBag.Message = "The Picture Has Been Saved Successfully in your Profile!";
             return(View("Common"));
         }
         return(RedirectToAction(nameof(Index)));
     }
     catch (System.Exception)
     {
         ViewBag.Message = "Unexpected error when saving the Picture in the Database. Try again later!";
         return(RedirectToAction(nameof(Index)));
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Transform a picture view object to a Category EF Obj
 /// </summary>
 /// <param name="picture"></param>
 /// <returns></returns>
 public static DbObj.Picture TransformViewToDbObj(ViewObj.Picture picture)
 {
     DbObj.Picture newPicture = null;
     if (picture != null)
     {
         newPicture = new DbObj.Picture
         {
             PictureId   = picture.PictureId,
             CategoryId  = picture.CategoryId,
             Name        = picture.Name,
             Description = picture.Description,
             FileName    = picture.FileName,
             SourceUrl   = picture.SourceUrl,
             Height      = picture.Height,
             Width       = picture.Width,
             Image       = null,             // Field that needs to be setup from outside the transformation
             UserEmail   = string.Empty      // Field that needs to be setup from outside the transformation
         };
     }
     return(newPicture);
 }
Exemplo n.º 6
0
 public IActionResult Delete(string id)
 {
     try
     {
         int picId = Convert.ToInt32(id);
         var model = new ObjView.Picture();
         if (picId > 0)
         {
             // Calling the DB context to delet the picture
             _pictureService.DeleteGifAnimetedFromDB(picId);
         }
         ViewBag.IsError = false;
         ViewBag.Message = "Category deleted sucessfully!";
         return(View("Common"));
     }
     catch (Exception)
     {
         ViewBag.IsError = true;
         ViewBag.Message = $"Unexpected error ocurred while Deleting the Category for user: '******'. Try again later!";
         return(RedirectToAction(nameof(Index)));
     }
 }