public HttpResponseMessage Post(EmployeeCost value)
        {
            HttpResponseMessage response = null;

            ValidateModel(value);

            if (!ModelState.IsValid)
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, GetModelErrors());
                return(response);
            }

            try
            {
                service.Save(value);
                response = Request.CreateResponse(HttpStatusCode.Created, value);
                string uri = Url.Link("DefaultApi", new { id = value.Id });
                response.Headers.Location = new Uri(uri);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

            return(response);
        }
        public async Task <IActionResult> Edit(int id, [Bind("CostId,DailyRate,EmployeeNumber,HourlyRate,MonthlyIncome,MonthlyRate,OverTime,PercentSalaryHike,StandardHours")] EmployeeCost employeeCost)
        {
            if (id != employeeCost.CostId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(employeeCost);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EmployeeCostExists(employeeCost.CostId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["EmployeeNumber"] = new SelectList(_context.EmployeeDetails, "EmployeeNumber", "EmployeeNumber", employeeCost.EmployeeNumber);
            return(View(employeeCost));
        }
Esempio n. 3
0
        public void Post_ReturnsBadRequest_IfValueIsInvalid()
        {
            var cost     = new EmployeeCost();
            var response = controller.Post(cost);

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }
 public BudgetEmployeeCostDefinedEvent(
     Guid budgetId,
     EmployeeCost employeeCost)
 {
     BudgetId     = budgetId;
     EmployeeCost = employeeCost;
 }
Esempio n. 5
0
        public void Can_GetByDate()
        {
            DateTime today = DateTime.Today;
            var      cost  = new EmployeeCost();

            costServiceMock.Setup(s => s.GetByDate(today)).Returns(cost);
            Assert.Equal(cost, controller.GetByDate(today));
        }
Esempio n. 6
0
        public void GetByDate_ThrowsErrorIfNotFound()
        {
            DateTime     today = DateTime.Today;
            EmployeeCost cost  = null;

            costServiceMock.Setup(s => s.GetByDate(today)).Returns(cost);
            Assert.Throws <HttpResponseException>(() => controller.GetByDate(today));
        }
Esempio n. 7
0
        public void Can_GetById()
        {
            Guid id   = Guid.NewGuid();
            var  cost = new EmployeeCost();

            costServiceMock.Setup(s => s.Get(id)).Returns(cost);
            Assert.Equal(cost, controller.Get(id));
        }
Esempio n. 8
0
        public void GetById_ThrowsErrorIfNotFound()
        {
            Guid         id   = Guid.NewGuid();
            EmployeeCost cost = null;

            costServiceMock.Setup(s => s.Get(id)).Returns(cost);
            Assert.Throws <HttpResponseException>(() => controller.Get(id));
        }
        public async Task <IActionResult> Create([Bind("CostId,DailyRate,EmployeeNumber,HourlyRate,MonthlyIncome,MonthlyRate,OverTime,PercentSalaryHike,StandardHours")] EmployeeCost employeeCost)
        {
            if (ModelState.IsValid)
            {
                _context.Add(employeeCost);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["EmployeeNumber"] = new SelectList(_context.EmployeeDetails, "EmployeeNumber", "EmployeeNumber", employeeCost.EmployeeNumber);
            return(View(employeeCost));
        }
Esempio n. 10
0
        //TODO:
        //find a way to mock Url.Link
        //[Fact]
        public void Post_SaveEmployeeCost()
        {
            var cost = new EmployeeCost
            {
                Date    = DateTime.Today,
                Total   = 100,
                Details = new List <EmployeeCostDetail>()
                {
                    new EmployeeCostDetail {
                        EmployeeId = Guid.NewGuid(), Present = true, Salary = 100
                    }
                }
            };
            var response = controller.Post(cost);

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
        }
Esempio n. 11
0
        public void Post_ReturnsInternalServerError_IfSavingFailed()
        {
            var cost = new EmployeeCost
            {
                Date    = DateTime.Today,
                Total   = 100,
                Details = new List <EmployeeCostDetail>()
                {
                    new EmployeeCostDetail {
                        EmployeeId = Guid.NewGuid(), Present = true, Salary = 100
                    }
                }
            };

            costServiceMock.Setup(c => c.Save(cost)).Throws(new Exception());
            var response = controller.Post(cost);

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
        }
        public HttpResponseMessage Put(Guid id, EmployeeCost value)
        {
            ValidateModel(value);

            if (!ModelState.IsValid || value.Id != id)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, GetModelErrors()));
            }

            HttpResponseMessage response = null;

            try
            {
                service.Save(value);
                response = Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

            return(response);
        }
        public void Save(EmployeeCost cost)
        {
            using (var db = factory.OpenDbConnection())
            {
                using (var tx = db.OpenTransaction())
                {
                    bool isNew = cost.IsNew;

                    Models.Data.EmployeeCost costData = null;

                    if (isNew)
                    {
                        costData = db.FirstOrDefault <Models.Data.EmployeeCost>(u => u.Date == cost.Date);
                    }
                    else
                    {
                        costData = db.Query <Models.Data.EmployeeCost>("Date = @Date and Id <> @Id",
                                                                       new { Date = cost.Date, Id = cost.Id.ToString() })
                                   .FirstOrDefault();
                    }
                    if (costData != null)
                    {
                        tx.Rollback();
                        throw new ServiceException("EmployeeCost_DuplicateDate");
                    }

                    if (isNew)
                    {
                        cost.Id = Guid.NewGuid();
                    }

                    costData = Mapper.Map <EmployeeCost, Models.Data.EmployeeCost>(cost);

                    if (isNew)
                    {
                        db.InsertParam(costData);
                    }
                    else
                    {
                        db.UpdateParam(costData);
                    }

                    if (!isNew)
                    {
                        db.Delete <Models.Data.EmployeeCostDetail>(where : "CostId = {0}".Params(costData.Id.ToString()));
                    }

                    foreach (var detail in cost.Details)
                    {
                        var detailData = Mapper.Map <EmployeeCostDetail, Models.Data.EmployeeCostDetail>(detail);
                        detailData.CostId = cost.Id;
                        db.InsertParam(detailData);
                    }
                    try
                    {
                        tx.Commit();
                    }
                    catch (Exception ex)
                    {
                        tx.Rollback();
                        throw new ServiceException(ex.Message);
                    }
                }
            }
        }