public bool Update(int id, AccountCreateApiModel apiModel, int modifiedUser) { var validator = _accountValidator.Validate(apiModel); if (validator.IsValid) { return(_accountRepository.Update(id, apiModel, modifiedUser)); } return(false); }
public bool Create(AccountCreateApiModel apiModel, int createdUser) { var validator = _accountValidator.Validate(apiModel); if (validator.IsValid) { return(_accountRepository.Create(apiModel, createdUser)); } return(false); }
public bool Update(int accountId, AccountCreateApiModel apiModel, int modifiedUser) { var dbAccount = db.ACCOUNTs.Find(accountId); if (dbAccount != null) { if (apiModel.owner != 0) { dbAccount.AccountOwner = apiModel.owner; } if (apiModel.collaborator != 0) { dbAccount.AccountCollaborator = apiModel.collaborator; } dbAccount.Name = apiModel.name; dbAccount.Email = apiModel.email; dbAccount.Phone = apiModel.phone; dbAccount.Fax = apiModel.fax; dbAccount.TaxCode = apiModel.taxCode; dbAccount.NoEmployees = apiModel.numberOfEmployees; dbAccount.AnnualRevenue = apiModel.annualRevenue; dbAccount.Website = apiModel.website; dbAccount.BankName = apiModel.bankName; dbAccount.BankAccountName = apiModel.bankAccountName; dbAccount.BankAccount = apiModel.bankAccount; dbAccount.Country = apiModel.country; dbAccount.City = apiModel.city; dbAccount.AddressDetail = apiModel.addressDetail; dbAccount.ModifiedAt = DateTime.Now; dbAccount.ModifiedBy = modifiedUser; db.SaveChanges(); var owner = db.USERs.Find(dbAccount.AccountOwner); var collaborator = db.USERs.Find(dbAccount.AccountCollaborator); var modifyUser = db.USERs.Find(modifiedUser); var creator = db.USERs.Find(dbAccount.CreatedBy); var notifyModel = new NotificationApiModel(); notifyModel.title = "Account updated"; notifyModel.content = $"Account {dbAccount.Name} has been updated by {modifyUser.Username}."; notifyModel.module = "accounts"; notifyModel.moduleObjectId = dbAccount.ID; notifyModel.createdAt = DateTime.Now; NotificationManager.SendNotification(notifyModel, new List <USER> { owner, collaborator, creator }); return(true); } else { return(false); } }
public bool Create(AccountCreateApiModel apiModel, int createdUser) { var newAccount = new ACCOUNT(); newAccount.AccountOwner = apiModel.owner != 0 ? apiModel.owner : createdUser; newAccount.Name = apiModel.name; newAccount.Email = apiModel.email; newAccount.Phone = apiModel.phone; newAccount.Fax = apiModel.fax; newAccount.TaxCode = apiModel.taxCode; newAccount.NoEmployees = apiModel.numberOfEmployees; newAccount.AnnualRevenue = apiModel.annualRevenue; newAccount.Website = apiModel.website; newAccount.BankName = apiModel.bankName; newAccount.BankAccountName = apiModel.bankAccountName; newAccount.BankAccount = apiModel.bankAccount; newAccount.Country = apiModel.country; newAccount.City = apiModel.city; newAccount.AddressDetail = apiModel.addressDetail; newAccount.CreatedAt = DateTime.Now; newAccount.CreatedBy = createdUser; newAccount.ModifiedAt = DateTime.Now; if (apiModel.collaborator != 0) { newAccount.AccountCollaborator = apiModel.collaborator; } try { db.ACCOUNTs.Add(newAccount); db.SaveChanges(); var owner = db.USERs.Find(newAccount.AccountOwner); var collaborator = db.USERs.Find(newAccount.AccountCollaborator); var creator = db.USERs.Find(createdUser); var notifyModel = new NotificationApiModel(); notifyModel.title = "Account assigned"; notifyModel.content = $"Account {newAccount.Name} has been created and assigned to you by {creator?.Username}."; notifyModel.createdAt = DateTime.Now; notifyModel.module = "accounts"; notifyModel.moduleObjectId = newAccount.ID; NotificationManager.SendNotification(notifyModel, new List <USER> { owner, collaborator }); return(true); } catch { return(false); } }
public HttpResponseMessage Create(AccountCreateApiModel apiModel) { var response = new HttpResponseMessage(); ResponseFormat responseData = new ResponseFormat(); AuthorizationService _authorizationService = new AuthorizationService().SetPerm((int)EnumPermissions.ACCOUNT_CREATE); //read jwt IEnumerable <string> headerValues; if (Request.Headers.TryGetValues("Authorization", out headerValues)) { string jwt = headerValues.FirstOrDefault(); //validate jwt var payload = JwtTokenManager.ValidateJwtToken(jwt); if (payload.ContainsKey("error")) { if ((string)payload["error"] == ErrorMessages.TOKEN_EXPIRED) { response.StatusCode = HttpStatusCode.Unauthorized; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.TOKEN_EXPIRED; } if ((string)payload["error"] == ErrorMessages.TOKEN_INVALID) { response.StatusCode = HttpStatusCode.Unauthorized; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.TOKEN_INVALID; } } else { var userId = payload["id"]; var isAuthorized = _authorizationService.Authorize(Convert.ToInt32(userId)); if (isAuthorized) { var isCreated = _accountService.Create(apiModel, Convert.ToInt32(userId)); if (isCreated) { response.StatusCode = HttpStatusCode.OK; responseData = ResponseFormat.Success; responseData.message = SuccessMessages.ACCOUNT_CREATED; } } else { response.StatusCode = HttpStatusCode.Forbidden; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.UNAUTHORIZED; } } } else { response.StatusCode = HttpStatusCode.Unauthorized; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.UNAUTHORIZED; } var json = JsonConvert.SerializeObject(responseData); response.Content = new StringContent(json, Encoding.UTF8, "application/json"); return(response); }
public HttpResponseMessage Update([FromUri] int id, [FromBody] AccountCreateApiModel apiModel) { var response = new HttpResponseMessage(); ResponseFormat responseData = new ResponseFormat(); //AuthorizationService _authorizationService = new AuthorizationService().SetPerm((int)EnumPermissions.LEAD_MODIFY); //read jwt IEnumerable <string> headerValues; if (Request.Headers.TryGetValues("Authorization", out headerValues)) { string jwt = headerValues.FirstOrDefault(); //validate jwt var payload = JwtTokenManager.ValidateJwtToken(jwt); if (payload.ContainsKey("error")) { if ((string)payload["error"] == ErrorMessages.TOKEN_EXPIRED) { response.StatusCode = HttpStatusCode.Unauthorized; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.TOKEN_EXPIRED; } if ((string)payload["error"] == ErrorMessages.TOKEN_INVALID) { response.StatusCode = HttpStatusCode.Unauthorized; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.TOKEN_INVALID; } } else { var userId = Convert.ToInt32(payload["id"]); var owner = _accountService.FindOwnerId(id); var collaborator = _accountService.FindCollaboratorId(id); if ((userId == owner) || (userId == collaborator) || (new AuthorizationService().SetPerm((int)EnumPermissions.ACCOUNT_DELETE).Authorize(userId))) { var isUpdated = _accountService.Update(id, apiModel, Convert.ToInt32(userId)); if (isUpdated) { response.StatusCode = HttpStatusCode.OK; responseData = ResponseFormat.Success; responseData.message = SuccessMessages.ACCOUNT_MODIFIED; } else { response.StatusCode = HttpStatusCode.InternalServerError; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.SOMETHING_WRONG; } } else { response.StatusCode = HttpStatusCode.Forbidden; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.UNAUTHORIZED; } } } else { response.StatusCode = HttpStatusCode.Unauthorized; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.UNAUTHORIZED; } var json = JsonConvert.SerializeObject(responseData); response.Content = new StringContent(json, Encoding.UTF8, "application/json"); return(response); }