0

I am basically trying to handle unique constraint validation in my .Net API. I have two unique key cosntraints on two fields in my table.

enter image description here

If you see my code below , I am trying to check if the record exists. I am looking at returning a boolean value if the record exist. is that possible. For the time being, I am returning null.

Is this the best way to do it.

[HttpPost]
        [SkipTokenAuthorization]
        [Route("api/classificationoverrides/create")]
        public IHttpActionResult Create(ClassificationItemViewModelCreate model)
        {
            var mgrClassificationService = GetService<MGR_STRT_CLASSIFICATION>();

            var isExists = mgrClassificationService.Where(x =>
                x.MANAGERSTRATEGYID == model.ManagerStrategyId && x.PRODUCT_ID == model.ProductId).FirstOrDefault();

            if (isExists == null)
            {

                var mgrClassficationOverride = new MGR_STRT_CLASSIFICATION();
                if (model != null)
                {
                    mgrClassficationOverride.PRODUCT_ID = model.ProductId;
                    mgrClassficationOverride.LEGACY_STRATEGY_ID = model.LegacyStrategyId;
                    mgrClassficationOverride.STRATEGY_ID = model.StrategyId;
                    mgrClassficationOverride.MANAGERSTRATEGY_TYPE_ID = model.ManagerStrategyTypeId;
                    mgrClassficationOverride.MANAGERSTRATEGYID = model.ManagerStrategyId;

                    mgrClassficationOverride = mgrClassificationService.Create(mgrClassficationOverride);
                }
                return Ok(mgrClassficationOverride);
            }
            else
            {
                return null;
            }
        }
Tom
  • 8,175
  • 41
  • 136
  • 267
  • 1
    Possible duplicate of [HTTP response code for POST when resource already exists](https://stackoverflow.com/questions/3825990/http-response-code-for-post-when-resource-already-exists) – Owen Pauling Apr 25 '19 at 09:01
  • I am actually not clear with the link you shared. – Tom Apr 25 '19 at 09:38
  • I am not handling be throwing exceptions but rather by sending a bool value. My question is how do i do it with HttpActionResult – Tom Apr 25 '19 at 09:39

0 Answers0