public async Task <ActionResult> Delete(AdvertiseViewModel model) { try { if (!await APIProvider.Authorization(_userSession.BearerToken, ARS.Delete)) { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.FuntionType.Article, APIConstant.ACTION_DELETE); return(RedirectToAction("Index")); } else { return(PartialView("_Delete", model)); } } catch (HttpException ex) { Logger.LogError(ex); int statusCode = ex.GetHttpCode(); if (statusCode == 401) { TempData["Alert"] = ApplicationGenerator.RenderResult(FuntionType.Department, APIConstant.ACTION_ACCESS); return(new HttpUnauthorizedResult()); } throw ex; } }
private static List <AdvertiseViewModel> ConvertToViewModel(List <Advertise> entities) { List <AdvertiseViewModel> viewModels = new List <AdvertiseViewModel>(); List <string> idtypes = entities.Select(mod => { return(mod.idat); }).ToList(); idtypes = idtypes.Distinct().ToList(); TN db = new TN(); List <AdvertiseType> advertiseTypes = db.AdvertiseTypes.Where(en => idtypes.Contains(en.idat)).ToList(); viewModels = entities.Select(mod => { AdvertiseViewModel viewModel = new AdvertiseViewModel(); viewModel.CopyFromBase(mod); if (advertiseTypes != null && advertiseTypes.Count > 0) { List <AdvertiseType> tempAdvertiseTypes = advertiseTypes.Where(en => en.idat == viewModel.idat).ToList(); AdvertiseType tempAdvertiseType = (tempAdvertiseTypes != null && tempAdvertiseTypes.Count > 0) ? tempAdvertiseTypes.First() : null; AdvertiseTypeViewModel tempAdvertiseTypeModel = new AdvertiseTypeViewModel(); if (tempAdvertiseType != null) { tempAdvertiseTypeModel.CopyFromBase(tempAdvertiseType); viewModel.AdvertiseType = tempAdvertiseTypeModel; } } return(viewModel); }).ToList(); return(viewModels); }
public async Task <IActionResult> Post(string makename, string modelname, [FromBody] AdvertiseViewModel model) { try { var modelCar = _modelCarRepo.GetModelByName(modelname); if (modelCar == null) { return(BadRequest("No add for this modelname")); } var advertise = _mapper.Map <Advertise>(model); advertise.ModelCar = modelCar; advertise.DateAdded = DateTime.Now; //advertise.DateUpdated = DateTime.Now; var personWhoAdded = await _usrMgr.FindByNameAsync(this.User.Identity.Name); if (personWhoAdded != null) { advertise.UserWhoAdded = personWhoAdded.UserName; _advertiseRepo.Add(advertise); if (await _advertiseRepo.SaveAllAsync()) { return(Created(Url.Link("GetAdvertise", new { makename = makename, modelname = modelname, id = advertise.Id }), _mapper.Map <AdvertiseViewModel>(advertise))); } } } catch { } return(BadRequest("Failed to save new talk")); }
public async Task <ActionResult> Edit(AdvertiseViewModel model, HttpPostedFileBase fileUpload) { var token = _userSession.BearerToken; if (fileUpload == null && String.IsNullOrEmpty(model.Resouce)) { ModelState.AddModelError("Resouce", "Vui lòng chọn hình ảnh"); } if (ModelState.IsValid) { if (fileUpload != null) { string name = ""; if (fileUpload.ContentType.Contains("image")) { FileManagement.UploadImage(fileUpload, ValueConstant.IMAGE_ADVERTISE_PATH, ref name); model.Type = (byte)ValueConstant.MEDIA_TYPE.IMAGE; model.Resouce = name; } else { FileManagement.UploadFile(fileUpload, ValueConstant.IMAGE_ADVERTISE_PATH, ref name); string pathVideo = Server.MapPath(name); model.Type = (byte)ValueConstant.MEDIA_TYPE.VIDEO; string pathThumbnail = Server.MapPath(ValueConstant.IMAGE_ADVERTISE_PATH); var ffMpeg = new NReco.VideoConverter.FFMpegConverter(); string[] arrTemp = fileUpload.FileName.Split('.'); string fileName = pathThumbnail + "/" + arrTemp[0] + ".jpg"; ffMpeg.GetVideoThumbnail(pathVideo, fileName, 10.0f); model.Resouce = name.Replace(fileUpload.FileName, arrTemp[0] + ".jpg"); } } //Call API Provider string strUrl = APIProvider.APIGenerator(controllerName, APIConstant.ACTION_UPDATE); var result = await APIProvider.Authorize_DynamicTransaction <AdvertiseViewModel, bool>(model, token, strUrl, APIConstant.API_Resource_CMS, ARS.IgnoredARS); if (result) { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.SUCCESS, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_UPDATE, ApplicationGenerator.TypeResult.SUCCESS)); } else { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.FAIL, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_UPDATE, ApplicationGenerator.TypeResult.FAIL)); } return(RedirectToAction("Index")); } else { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.FAIL, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_UPDATE, ApplicationGenerator.TypeResult.FAIL)); return(View(model)); } }
public async Task <AdvertiseViewModel> Get(byte id) { var list = this.RequestContext.RouteData.Values.Keys; var para = APIProvider.APIGeneratorParameter(list, id); var source = await _advertise.SingleQuery(para); AdvertiseViewModel dest = Mapper.Map <AdvertiseViewModel>(source); return(dest); }
public ActionResult Create(AdvertiseViewModel model) { if (Request.Cookies["MagazineId"].Value == null) { SetMessage("Lo sentimos, ha ocurrido un error. Inténtelo de nuevo.", BootstrapAlertTypes.Danger); return(RedirectToAction("Index", "Magazines")); } if (!UserService.UserInMagazine(int.Parse(Request.Cookies["MagazineId"].Value), UserService.GetCurrentUser().UserId)) { return(RedirectToAction("Index", "Magazines")); } if (!ModelState.IsValid) { SetMessage("Lo sentimos, favor de verificar los datos. Inténtelo de nuevo.", BootstrapAlertTypes.Danger); return(View(model)); } var imageCode = "Expose_Default_New.jpg"; if (model.Image != null && model.Image.ContentLength > 0) { var imageModel = ResourceService.SaveImage(Server.MapPath("~/content/data/"), model.Image, false); if (imageModel == null) { ModelState.AddModelError("", "No se pudo guardar la imagen. Intentalo de nuevo."); return(View(model)); } imageCode = imageModel.FullFileName; } var create = MagazineService.CreateAd(model, imageCode, null); if (!create) { SetMessage("Ocurrió un error inesperado. Inténtelo de nuevo.", BootstrapAlertTypes.Danger); return(View(model)); } SetMessage("Anuncio creado exitosamente", BootstrapAlertTypes.Success); return(RedirectToAction("Index", new { id = model.AdCategoryId })); }
public ActionResult Edit(int?id) { if (Request.Cookies["MagazineId"].Value == null) { SetMessage("Lo sentimos, ha ocurrido un error. Inténtelo de nuevo.", BootstrapAlertTypes.Danger); return(RedirectToAction("Index", "Magazines")); } if (!UserService.UserInMagazine(int.Parse(Request.Cookies["MagazineId"].Value), UserService.GetCurrentUser().UserId)) { return(RedirectToAction("Index", "Magazines")); } if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Advertise advertise = db.AdvertisesList.SingleOrDefault(x => x.AdvertiseId == id); if (advertise == null) { return(HttpNotFound()); } var model = new AdvertiseViewModel() { AdvertiseId = advertise.AdvertiseId, AdCategoryId = advertise.AdCategoryId.Value, Content = advertise.Content, IFrame = advertise.IFrame, ImageString = advertise.Image, Name = advertise.Name, Url = advertise.Url, Campaign = advertise.Campaign, Horizontal = advertise.Horizontal, Medium = advertise.Medium, Source = advertise.Source, Term = advertise.Term }; return(View(model)); }
public async Task <ActionResult> Edit(short id) { try { var model = new AdvertiseViewModel(); //Call API Provider string strUrl = APIProvider.APIGenerator(this, this.RouteData.Values["action"].ToString(), id); model = await APIProvider.Authorize_Get <AdvertiseViewModel>(_userSession.BearerToken, controllerName + strUrl, APIConstant.API_Resource_CMS, ARS.Get); return(View(model)); } catch (HttpException ex) { Logger.LogError(ex); int statusCode = ex.GetHttpCode(); if (statusCode == 401) { TempData["Alert"] = ApplicationGenerator.RenderResult(FuntionType.Department, APIConstant.ACTION_ACCESS); return(new HttpUnauthorizedResult()); } throw ex; } }
public async Task <ActionResult> Create(AdvertiseViewModel model, HttpPostedFileBase fileUpload) { try { var token = _userSession.BearerToken; if (fileUpload == null) { ModelState.AddModelError("Resouce", "Vui lòng chọn video hoặc hình ảnh"); } if (ModelState.IsValid) { if (fileUpload != null) { string name = ""; if (fileUpload.ContentType.Contains("image")) { FileManagement.UploadImage(fileUpload, ValueConstant.IMAGE_ADVERTISE_PATH, ref name); model.Type = (Byte)ValueConstant.MEDIA_TYPE.IMAGE; model.Resouce = name; } else { FileManagement.UploadFile(fileUpload, ValueConstant.IMAGE_ADVERTISE_PATH, ref name); string pathVideo = Server.MapPath(name); model.Type = (byte)ValueConstant.MEDIA_TYPE.VIDEO; UploadVideo(fileUpload, pathVideo); model.Resouce = name; } } //Call API Provider string strUrl = APIProvider.APIGenerator(controllerName, APIConstant.ACTION_INSERT); var result = await APIProvider.Authorize_DynamicTransaction <AdvertiseViewModel, bool>(model, token, strUrl, APIConstant.API_Resource_CMS, ARS.Insert); if (result) { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.SUCCESS, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_INSERT, ApplicationGenerator.TypeResult.SUCCESS)); } else { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.FAIL, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_INSERT, ApplicationGenerator.TypeResult.FAIL)); } return(RedirectToAction("Index")); } else { var slider = new AdvertisesModel(); slider.lstAdvertiseViewModel = await APIProvider.Authorize_Get <List <AdvertiseViewModel> >(token, controllerName, APIConstant.API_Resource_CMS); slider.AdvertiseViewModel = model; TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.FAIL, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_INSERT, ApplicationGenerator.TypeResult.FAIL)); return(View("Index", slider)); } } catch (HttpException ex) { Logger.LogError(ex); int statusCode = ex.GetHttpCode(); if (statusCode == 401) { TempData["Alert"] = ApplicationGenerator.RenderResult(FuntionType.Department, APIConstant.ACTION_ACCESS); return(new HttpUnauthorizedResult()); } throw ex; } }
public ActionResult ViewVideo(AdvertiseViewModel model) { model.Resouce = model.Resouce.Replace(@"\", @"/"); return(PartialView("_ViewVideo", model)); }
public async Task <bool> Transaction(AdvertiseViewModel advertiseModel, char action) { var data = Mapper.Map <Domain.Models.CMS.Advertise>(advertiseModel); return(await _advertise.Transaction(data, action)); }
public async Task <IActionResult> Put(string makename, string modelname, int id, [FromBody] AdvertiseViewModel vm) { try { var advertise = _advertiseRepo.GetAdvertiseById(id); if (advertise == null) { return(NotFound()); } if (advertise.ModelCar.Name != modelname) { return(BadRequest("Make and model dont match ")); } vm.DateAdded = advertise.DateAdded; vm.DateUpdated = DateTime.Now; if (vm.UserWhoAdded != this.User.Identity.Name) { return(Forbid()); } _mapper.Map(vm, advertise); if (await _advertiseRepo.SaveAllAsync()) { return(Ok(_mapper.Map <AdvertiseViewModel>(advertise))); } } catch {} return(BadRequest("Failed to update the add")); }