示例#1
0
        public async Task <IActionResult> CreateWorkhoursAsync([FromBody] WorkhoursCreateDto workhoursToCreate)
        {
            var workhoursToAdd = Mapper.Map <Workhours>(workhoursToCreate);

            workhoursToAdd.Id = Guid.NewGuid();

            _workhoursRepository.Create(workhoursToAdd);

            await _workhoursRepository.SaveChangesAsync();

            var foundWorkhours = await _workhoursRepository.GetFirstByConditionAsync(j => j.Id == workhoursToAdd.Id);

            return(Ok());
        }
示例#2
0
        public async Task <IActionResult> CreateWorkhoursAsync(
            [FromBody, SwaggerParameter("Workhours to create", Required = true)] WorkhoursCreateDto workhoursToCreate,
            [FromQuery, SwaggerParameter("Request which fields you want returned")] string fields,
            [FromHeader(Name = "accept"), SwaggerParameter("Request Hateoas")] string mediaType)
        {
            var workhoursToAdd = Mapper.Map <Workhours>(workhoursToCreate);

            workhoursToAdd.Id = Guid.NewGuid();

            _workhoursRepository.Create(workhoursToAdd);

            if (!await _workhoursRepository.SaveChangesAsync())
            {
                //  change to logging
                throw new Exception("Failed to save workhours");
                //  consider to return an error to notify user of failed save
            }

            var foundWorkhours = await _workhoursRepository.GetFirstByConditionAsync(j => j.Id == workhoursToAdd.Id);

            var workhoursToReturn = Mapper.Map <WorkhoursDto>(foundWorkhours);

            if (mediaType == "application/vnd.AO.json+hateoas")
            {
                var shapedWorkhours = _controllerHelper.ShapeAndAddLinkToObject(workhoursToReturn, "Workhours", fields);

                return(CreatedAtRoute("GetWorkhour", new { workhoursId = workhoursToReturn.Id }, shapedWorkhours));
            }

            if (!string.IsNullOrWhiteSpace(fields))
            {
                var shapedWorkhours = workhoursToReturn.ShapeData(fields);

                return(CreatedAtRoute("GetWorkhour", new { workhoursId = workhoursToReturn.Id }, shapedWorkhours));
            }

            return(CreatedAtRoute("GetWorkhour", new { workhoursId = workhoursToReturn.Id }, workhoursToReturn));
        }