예제 #1
0
        public async Task <IHttpCallResultCGHT <xDTO.Country> > UpdateCountryAsync(xDTO.Country item)
        {
            var retVal = await SerializationHelper.Instance.SerializeCallResultsPut <xDTO.Country>(
                Log, HttpClient,
                $"CSC/Countries/{item.CountryId}", item);

            return(retVal);
        }
        public async Task <IHttpActionResult> Put(Guid countryId, [FromBody] dtoCSC.Country dtoItem)
        {
            try
            {
                if (!base.OnActionExecuting(out HttpStatusCode httpStatusCode, out string message))
                {
                    return(Content(httpStatusCode, message));
                }

                if (dtoItem == null)
                {
                    return(BadRequest());
                }

                dtoItem.CountryId = countryId;

                var updatedDBItem = _factory.Create(dtoItem);                 // map
                RunCustomLogicBeforeUpdatePut(ref updatedDBItem, countryId);
                var result = await Repo.UpdateAsync(updatedDBItem);

                RunCustomLogicAfterUpdatePut(ref updatedDBItem, ref result);

                if (result.Status == cghEnums.RepositoryActionStatus.Updated)
                {
                    // map to dto
                    var updatedDTOItem = _factory.Create(result.Entity);
                    return(Ok(updatedDTOItem));
                }
                else if (result.Status == cghEnums.RepositoryActionStatus.NotFound)
                {
                    return(NotFound());
                }

                Log.LogWarning(eventId: (int)coreEnums.EventId.Warn_WebApi,
                               exception: result.Exception,
                               message: "Unable to update object via Web API for RequestUri {{RequestUri}}",
                               Request.RequestUri.ToString());

                return(BadRequest());
            }
            catch (Exception ex)
            {
                Log.LogError(eventId: (int)coreEnums.EventId.Exception_WebApi,
                             exception: ex,
                             message: "Unable to update object via Web API for RequestUri {{RequestUri}}",
                             Request.RequestUri.ToString());

                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debugger.Break();
                }

                return(InternalServerError());
            }
        }
        public async Task <IHttpActionResult> Post([FromBody] dtoCSC.Country dtoItem)
        {
            try
            {
                if (!base.OnActionExecuting(out HttpStatusCode httpStatusCode, out string message))
                {
                    return(Content(httpStatusCode, message));
                }

                if (dtoItem == null)
                {
                    return(BadRequest());
                }

                // try mapping & saving
                var newDBItem = _factory.Create(dtoItem);

                var result = await Repo.InsertAsync(newDBItem);

                RunCustomLogicAfterInsert(ref newDBItem, ref result);

                if (result.Status == cghEnums.RepositoryActionStatus.Created)
                {                   // map to dto
                    var newDTOItem   = _factory.Create(result.Entity);
                    var uriFormatted = Request.RequestUri.ToString().EndsWith("/") == true?Request.RequestUri.ToString().Substring(0, Request.RequestUri.ToString().Length - 1) : Request.RequestUri.ToString();

                    return(Created($"{uriFormatted}/{newDTOItem.CountryId}", newDTOItem));
                }

                Log.LogWarning(eventId: (int)coreEnums.EventId.Warn_WebApi,
                               exception: result.Exception,
                               message: "Unable to create object via Web API for RequestUri {{RequestUri}}",
                               Request.RequestUri.ToString());

                return(BadRequest());
            }
            catch (Exception ex)
            {
                Log.LogError(eventId: (int)coreEnums.EventId.Exception_WebApi,
                             exception: ex,
                             message: "Unable to create object via Web API for RequestUri {{RequestUri}}",
                             Request.RequestUri.ToString());


                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debugger.Break();
                }

                return(InternalServerError());
            }
        }