public async Task <IHttpActionResult> PutEmployees(int id, Employees employees)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != employees.EmployeeID)
            {
                return(BadRequest());
            }

            db.Entry(employees).State = EntityState.Modified;

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
示例#2
0
        public async Task <IActionResult> PutEmployee(int id, Employee employee)
        {
            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());
        }
示例#3
0
        public async Task <ActionResult <Employee> > PostEmployees(Employee employee)
        {
            db.Empls.Add(employee);
            await db.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetEmployees), new { id = employee.Id }, employee));
        }
        public async Task <IHttpActionResult> PutDepartment(int id, Department department)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != department.DepartmentID)
            {
                return(BadRequest());
            }

            db.Entry(department).State = EntityState.Modified;

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IActionResult> Update([FromBody] Department deptoToUpdate)
        {
            var depto = await _deptoContext.Departments.SingleOrDefaultAsync(i => i.Id == deptoToUpdate.Id);

            if (depto == null)
            {
                return(NotFound());
            }
            var raiseDeptoNameChangedEvent = depto.Description != deptoToUpdate.Description;
            var oldName = depto.Description;

            // Update current department
            depto = deptoToUpdate;
            _deptoContext.Departments.Update(depto);

            if (raiseDeptoNameChangedEvent) // Save and publish integration event if Description has changed
            {
                //Create Integration Event to be published through the Event Bus
                var descriptionChangedEvent = new DepartmentNameChangedIntegrationEvent(depto.Id, deptoToUpdate.Description, oldName);

                // Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
                await _departmentIntegrationEventService.SaveEventAndDeptoContextChangesAsync(descriptionChangedEvent);

                // Publish through the Event Bus and mark the saved event as published
                await _departmentIntegrationEventService.PublishThroughEventBusAsync(descriptionChangedEvent);
            }
            else // Save updated product
            {
                await _deptoContext.SaveChangesAsync();
            }

            return(Ok());
        }
示例#6
0
        public async Task <IActionResult> PutWorker(int id, Worker worker)
        {
            if (id != worker.WorkerId)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
示例#7
0
        public async Task <IActionResult> PutDepartment(int id, Department department)
        {
            if (id != department.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
示例#8
0
        public async Task <ActionResult <Meeting> > PostMeetings(Meeting meeting)
        {
            db.Meetings.Add(meeting);
            await db.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetMeetings), new { id = meeting.Id }, meeting));
        }
        public async Task <IActionResult> Create([Bind("Id,Name")] Department department)
        {
            if (ModelState.IsValid)
            {
                _context.Add(department);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(department));
        }
示例#10
0
        public async Task <bool> Put(int Id, Employee department)
        {
            _context.Entry(department).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(false);
            }
            return(true);
        }
示例#11
0
        public async Task <IActionResult> CreateDepartment([FromBody] DepartmentModel department)
        {
            var item = new DepartmentModel()
            {
                IsActive = true,
                Name     = department.Name
            };

            _departmentContext.Departments.Add(item);

            await _departmentContext.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetDepartmentById), new { id = item.Id }, null));
        }
示例#12
0
        public async Task <IActionResult> DeleteDepartment(int id)
        {
            var dept = await _db.Departments.FirstOrDefaultAsync(d => d.Id == id);

            if (dept == null)
            {
                return(NotFound());
            }
            else
            {
                _db.Remove(dept);

                await _db.SaveChangesAsync();

                return(Ok(dept));
            }
        }
示例#13
0
        public async Task <IActionResult> ReassignContacts(int id, int newId)
        {
            string newName = (await _context.Department.FirstOrDefaultAsync(m => m.Id == newId)).Name;
            IEnumerable <KeyValuePair <string, string> > data = new RouteValueDictionary(new { newId, newName })
                                                                .Select(k => new KeyValuePair <string, string>(k.Key, k.Value?.ToString()));

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"Home/ReassignContacts/{id}")
            {
                Content = new FormUrlEncodedContent(data)
            };
            HttpClient          client   = _clientFactory.CreateClient("contacts");
            HttpResponseMessage response = await client.SendAsync(request);

            Department department = await _context.Department.FindAsync(id);

            _context.Department.Remove(department);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
示例#14
0
 public async Task SaveEventAndDeptoContextChangesAsync(IntegrationEvent evt)
 {
     //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
     //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
     await ResilientTransaction.New(_departmentContext)
     .ExecuteAsync(async() => {
         // Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction
         await _departmentContext.SaveChangesAsync();
         await _eventLogService.SaveEventAsync(evt, _departmentContext.Database.CurrentTransaction.GetDbTransaction());
     });
 }
        public async Task CreateRequestForCommandAsync <T>(Guid id)
        {
            var exists = await ExistAsync(id);

            var request = exists ?
                          throw new DepartmentDomainException($"Request with {id} already exists") :
                                new ClientRequest()
                                {
                                    Id   = id,
                                    Name = typeof(T).Name,
                                    Time = DateTime.UtcNow
                                };

            _context.Add(request);

            await _context.SaveChangesAsync();
        }