Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] HoluserDto data)
        {
            APIResult apiResult;
            Holuser   record = mapper.Map <Holuser>(data);

            if (record != null)
            {
                var result       = mapper.Map <HoluserDto>(record);
                var isSuccessful = await holuserService.AddAsync(record);

                if (isSuccessful)
                {
                    apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK,
                                                       ErrorMessageEnum.None, payload: result);
                }
                else
                {
                    apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                       ErrorMessageEnum.無法新增紀錄, payload: result);
                }
            }
            else
            {
                apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                   ErrorMessageEnum.傳送過來的資料有問題, payload: data);
            }
            return(Ok(apiResult));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Put([FromRoute] int id, [FromBody] HoluserDto data)
        {
            APIResult apiResult;
            var       record = await holuserService.GetAsync(id);

            if (record != null)
            {
                Holuser recordTarget = mapper.Map <Holuser>(data);
                recordTarget.HoluserId = id;
                var result       = mapper.Map <HoluserDto>(recordTarget);
                var isSuccessful = await holuserService.UpdateAsync(recordTarget);

                if (isSuccessful)
                {
                    apiResult = APIResultFactory.Build(true, StatusCodes.Status202Accepted,
                                                       ErrorMessageEnum.None, payload: result);
                }
                else
                {
                    apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                       ErrorMessageEnum.無法修改紀錄, payload: result);
                }
            }
            else
            {
                apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                   ErrorMessageEnum.沒有任何符合資料存在, payload: data);
            }
            return(Ok(apiResult));
        }