Exemple #1
0
        public async Task <IActionResult> Save([FromBody] CameraViewModel model)
        {
            try
            {
                CameraEntity entity = null;
                if (!ModelState.IsValid)
                {
                    return(Ok(new ResponseModel()
                    {
                        Result = ResultCode.NotValidData
                    }));
                }
                var item = await _dm.CameraAccessor.GetCamera(model.Producer, model.Model);

                if (item != null && item.Id != model.Id)
                {
                    return(Ok(new ResponseModel()
                    {
                        Result = ResultCode.AlreadyExists
                    }));
                }
                if (model.Id <= 0)
                {
                    entity = new CameraEntity();
                }
                else
                {
                    entity = await _dm.CameraAccessor.GetCamera(model.Id);

                    if (entity == null)
                    {
                        return(Ok(new ResponseModel()
                        {
                            Result = ResultCode.AlreadyExists
                        }));
                    }
                }
                var entityToSave = model.ToCameraEntity();

                await _dm.CameraAccessor.SaveCamera(entityToSave);

                return(Ok(new ResponseModel()
                {
                    Result = ResultCode.Success
                }));
            }
            catch (Exception ex)
            {
                return(Ok(new ResponseModel()
                {
                    Result = ResultCode.ServerError, Description = ex.Message
                }));
            }
        }