public HttpResponseMessage Post(LoyaltyCardModel model) { try { var entity = model.ToEnity(); entity.CreatedOn = DateTime.UtcNow; entity.ModifiedOn = DateTime.UtcNow; var CardTypes = _loyaltyCardTypeService.All(); var CardType = _loyaltyCardTypeService.Find(x => x.SpentThreshold <= entity.TotalSpent)?.FirstOrDefault(); entity.LoyaltyCardTypeId = CardType.Id; entity.StartDate = DateTime.UtcNow; if (CardType != null) { entity.EndDate = entity.StartDate.Value.AddDays(CardType.Duration); } else { entity.EndDate = entity.StartDate; } _loyaltyCardService.Insert(entity); return(Request.CreateResponse(HttpStatusCode.OK, "Card is Created!")); } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, e)); } }
public JsonMessageModel Post(TransactionModel model, string access_token = null) { if (!AuthorizeAccessToken(access_token).success) { return(AuthorizeAccessToken(access_token)); } try { if (!ModelState.IsValid) { return new JsonMessageModel { success = false, statusCode = (int)HttpStatusCode.OK, message = "model is valid!" } } ; model.CreatedOn = DateTime.UtcNow; var trans = model.ToEnity(); //curent Config var config = _loyaltyConfigService.Find(x => x.Active)?.FirstOrDefault(); if (config == null) { return new JsonMessageModel { success = false, statusCode = (int)HttpStatusCode.OK, message = "Config is not found" } } ; //curentCard ///Lay config ra, quy doi diem thuong theo hang card, spent adjust ---> pointAdujust //spentAdjust-->config(spentAdjust*config) var SpentAdjust = trans.SpentAdjust; var div = (float)(SpentAdjust) / config.ConfigValue; var PointAdjust = Math.Round((float)SpentAdjust / config.ConfigValue, 2); trans.PointAdjust = PointAdjust; var CardId = trans.LoyaltyCardId; var Card = _loyaltyCardService.GetById(CardId); if (Card == null) { return new JsonMessageModel { success = false, statusCode = (int)HttpStatusCode.OK, message = "Loyalty Card not Found found" } } ; Card.Point += PointAdjust; Card.TotalSpent += SpentAdjust; //Cap nhat thong tin cho Card _loyaltyCardService.Update(Card); //Luu giao dich _transacitonService.Insert(trans); return(new JsonMessageModel { success = true, statusCode = (int)HttpStatusCode.OK, message = $"Trade {trans.Id} created!" }); } catch (Exception e) { return(ExceptionReturnMessage(e)); } }
public JsonMessageModel Post(ConfigModel model, string access_token = null) { if (!AuthorizeAccessToken(access_token).success) { return(AuthorizeAccessToken(access_token)); } try { if (!ModelState.IsValid) { return new JsonMessageModel { success = false, statusCode = (int)HttpStatusCode.ExpectationFailed, message = "Model state not Valid" } } ; var activeConfigs = _configService.Find(x => x.Active); var config = model.ToEnity(); config.Active = true; config.CreatedOn = DateTime.UtcNow; _configService.Insert(config); if (activeConfigs != null && activeConfigs.Any()) { foreach (var active in activeConfigs) { active.Active = false; _configService.Update(active); } } return(new JsonMessageModel { success = true, statusCode = (int)HttpStatusCode.OK, message = "New Config is created!", data = config.Id }); } catch (Exception e) { return(ExceptionReturnMessage(e)); } }