Esempio n. 1
0
        public async Task <IActionResult> AddUser([FromBody] AddUserRequest request)
        {
            string method = nameof(AddUser);

            LogHandler.LogMethod(LogHandler.EventType.Call, Logger, method, request);
            SingleResponse <object> response = new SingleResponse <object>();

            try
            {
                if (!request.CheckValidation(response))
                {
                    return(response.ToHttpResponse(Logger, method));
                }

                var user = request.ToEntity();

                //var existingEntity = await DbContext.GetUnitcost(unit);
                //if (existingEntity != null)
                //{
                //    response.ErrorCode = (int)ErrorHandler.ErrorCode.AddRepeatedEntity;
                //    return response.ToHttpResponse(Logger, method);
                //}

                DbContext.Add(user);
                int save = await DbContext.SaveChangesAsync();

                if (save == 0)
                {
                    response.ErrorCode = (int)ErrorHandler.ErrorCode.DbSaveNotDone;
                    return(response.ToHttpResponse(Logger, method));
                }
                var entity_list = new List <User> {
                    user
                }
                .Select(x => new
                {
                    x.id,
                    x.create_date,
                    x.creator_id,
                    x.first_name,
                    x.last_name,
                    x.mobile,
                    x.status,
                    x.username
                }).First();
                response.Model     = entity_list;
                response.ErrorCode = (int)ErrorHandler.ErrorCode.OK;
            }
            catch (Exception ex)
            {
                LogHandler.LogError(Logger, response, method, ex);
            }
            return(response.ToHttpResponse(Logger, method));
        }
Esempio n. 2
0
        public async Task <IActionResult> EditUser([FromBody] AddUserRequest request)
        {
            string method = nameof(EditUser);

            LogHandler.LogMethod(LogHandler.EventType.Call, Logger, method, request);
            SingleResponse <object> response = new SingleResponse <object>();

            try
            {
                if (!request.CheckValidation(response))
                {
                    return(response.ToHttpResponse(Logger, method));
                }

                var entity = request.ToEntity();
                entity.id = request.id;

                var existingEntity = await DbContext.GetUser(entity);

                if (existingEntity == null)
                {
                    response.ErrorCode = (int)ErrorHandler.ErrorCode.NoContent;
                    return(response.ToHttpResponse(Logger, method));
                }

                existingEntity.first_name = entity.first_name;
                existingEntity.last_name  = entity.last_name;
                existingEntity.mobile     = entity.mobile;
                existingEntity.status     = entity.status;
                existingEntity.username   = entity.username;

                int save = await DbContext.SaveChangesAsync();

                if (save == 0)
                {
                    response.ErrorCode = (int)ErrorHandler.ErrorCode.DbSaveNotDone;
                    return(response.ToHttpResponse(Logger, method));
                }
                var entity_list = new List <User> {
                    entity
                }
                .Select(x => new
                {
                    x.id,
                    x.create_date,
                    x.creator_id,
                    x.first_name,
                    x.last_name,
                    x.mobile,
                    x.status,
                    x.username
                }).First();
                response.Model     = entity_list;
                response.ErrorCode = (int)ErrorHandler.ErrorCode.OK;
            }
            catch (Exception ex)
            {
                LogHandler.LogError(Logger, response, method, ex);
            }
            return(response.ToHttpResponse(Logger, method));
        }