public async Task <IActionResult> PutEmployee([FromRoute] int id, [FromBody] Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != employee.Id)
            {
                return(BadRequest());
            }

            _context.Entry(employee).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #2
0
        public async Task <IActionResult> Post([FromBody] Employee dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            _db.Employees.Add(dto);
            await _db.SaveChangesAsync();

            return(CreatedAtRoute("GetEmployee", new { id = dto.Id }, dto));
        }
Пример #3
0
        /// <summary>
        /// Insert
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async override Task <EmployeeModel> Insert(EmployeeModel request, ServerCallContext context)
        {
            if (request == null)
            {
                return(null);
            }

            var dbModel = new Models.Employee()
            {
                FirstName = request.FirstName,
                LastName  = request.LastName
            };

            _db.Employees.Add(dbModel);
            await _db.SaveChangesAsync();

            return(new EmployeeModel()
            {
                Id = dbModel.ID,
                FirstName = dbModel.FirstName,
                LastName = dbModel.LastName
            });
        }