public UsedActionModel Update(UsedActionModel model) { if (!_partnerService.Exists(model.PartnerId)) { throw new InvalidOperationException(ServicesConstants.UpdateUsedAction_NotAllowedReasonMessage_PartnerDoesNotExist); } if (!_actionService.Exists(model.ActionId)) { throw new InvalidOperationException(ServicesConstants.UpdateUsedAction_NotAllowedReasonMessage_ActionDoesNotExist); } if (!_userService.Exists(model.UserId)) { throw new InvalidOperationException(ServicesConstants.UpdateUsedAction_NotAllowedReasonMessage_UserDoesNotExist); } // remove this part if we want to allow multiple entries for same tripple // replace this part if we want to allow but limit var usedActions = _service.GetUsedActions(model.UserId, model.PartnerId, model.ActionId).Where(x => x.Id != model.Id).ToList(); if (usedActions.Count > 0) { throw new InvalidOperationException(ServicesConstants.UpdateUsedAction_NotAllowedReasonMessage_MapAlreadyExists); } var dMap = _service.GetUsedAction(model.Id); dMap.PartnerId = model.PartnerId; dMap.ActionId = model.ActionId; dMap.UserId = model.UserId; var ret = _service.Update(dMap); return(_mapper.Map <UsedAction, UsedActionModel>(ret)); }
public UsedActionModel Create(UsedActionModel model) { if (!_partnerService.Exists(model.PartnerId)) { throw new InvalidOperationException(ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_PartnerDoesNotExist); } if (!_actionService.Exists(model.ActionId)) { throw new InvalidOperationException(ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_ActionDoesNotExist); } if (!_userService.Exists(model.UserId)) { throw new InvalidOperationException(ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_UserDoesNotExist); } // remove this part if we want to allow multiple entries for same tripple // replace this part if we want to allow but limit if (_service.UsedActionExists(model.UserId, model.PartnerId, model.ActionId)) { throw new InvalidOperationException(ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_MapAlreadyExists); } var dModel = _mapper.Map <UsedActionModel, UsedAction>(model); dModel.DateCreated = DateTime.UtcNow; var ret = _service.Create(dModel); return(_mapper.Map <UsedAction, UsedActionModel>(ret)); }
public async Task <IActionResult> Create([Bind("Id,UserId,ActionId,PartnerId,ActionValue")] UsedActionModel usedAction) { if (ModelState.IsValid) { try { usedAction = _factory.Create(usedAction); return(RedirectToAction(nameof(Index))); } catch (InvalidOperationException e) { // log error here var allowedErrors = new List <string>() { ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_UserDoesNotExist, ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_PartnerDoesNotExist, ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_MapAlreadyExists, ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_ActionDoesNotExist, }; if (allowedErrors.Contains(e.Message ?? "")) { return(View("CustomError", new CustomErrorViewModel() { HeaderMessage = "Not allowed", Message = e.Message, ReturnUrls = new Dictionary <string, string>() { { "Back to Create Used Action Record page", Url.Action("Create", "UsedActions", new { area = "Admin" }) }, { "Go to Used Action Record List", Url.Action("Index", "UsedActions", new { area = "Admin" }) }, } })); } else { return(RedirectToAction(nameof(Index))); } } catch (Exception e) { // log error here return(RedirectToAction(nameof(Index))); } } else { ViewData["ActionId"] = new SelectList(_actionFactory.GetAll(), "Id", "Name", usedAction.ActionId); ViewData["PartnerId"] = new SelectList(_partnerFactory.GetAll(), "Id", "Name", usedAction.PartnerId); ViewData["UserId"] = new SelectList(_userFactory.GetAllUsers(), "Id", "UserName", usedAction.UserId); return(View(usedAction)); } }