Ejemplo n.º 1
0
        public void ValidateStep3Request(RequestOptionalViewModel request)
        {
            if (request.Photo != null)
            {
                String filename = request.Photo.FileName.ToLower();

                if (!filename.EndsWith(".jpg") && !filename.EndsWith(".jpeg") && !filename.EndsWith(".png"))
                {
                    throw new Exception("Invalid photo format. Must be JPG or PNG.");
                }
            }
        }
Ejemplo n.º 2
0
        public void SaveRequest(int userId, RequestMandatoryViewModel requestMandatory, IList <RequestItemViewModel> itemsToMerge = null, RequestOptionalViewModel requestOptional = null)
        {
            ValidateStep1Request(requestMandatory);

            RequestStatus status = RequestStatus.IN_PROGRESS;

            if (requestOptional != null)
            {
                ValidateStep3Request(requestOptional);
                status = RequestStatus.ACTIVE;
            }

            int categoryId = requestMandatory.CategoryId.Value;

            using (var context = new ApplicationContext())
            {
                var category = context.Categories.Where(x => x.Id == categoryId).SingleOrDefault();
                var area     = context.Areas.Where(x => x.Id == SessionFacade.AreaId).SingleOrDefault();

                Request entity;

                if (requestMandatory.Id != null)
                {
                    entity = context.Requests.Where(x => x.Id == requestMandatory.Id).SingleOrDefault();

                    // Caution! Accessing the request of another user!
                    if (userId != entity.UserId)
                    {
                        throw new UnauthorizedAccessException();
                    }

                    if (entity.Status == RequestStatus.COMPLETED)
                    {
                        throw new Exception("Request already completed");
                    }

                    // If already started or was suspendend, keep that way
                    if (entity.Status == RequestStatus.ACTIVE || entity.Status == RequestStatus.SUSPENDED)
                    {
                        status = entity.Status;
                    }

                    entity.Area        = area;
                    entity.Status      = status;
                    entity.DateDue     = requestMandatory.DateDue;
                    entity.Category    = category;
                    entity.Description = requestMandatory.Description;
                    entity.Subtitle    = requestMandatory.Subtitle;
                    entity.Title       = requestMandatory.Title;
                }
                else
                {
                    entity = new Request()
                    {
                        Area               = area,
                        Status             = status,
                        DateCreated        = DateTime.Now,
                        DateDue            = requestMandatory.DateDue,
                        Category           = category,
                        Description        = requestMandatory.Description,
                        Progress           = 0,
                        Subtitle           = requestMandatory.Subtitle,
                        Title              = requestMandatory.Title,
                        Items              = new List <RequestItem>(),
                        UserId             = userId,
                        VisualizationCount = 0
                    };

                    context.Requests.Add(entity);
                }

                if (itemsToMerge != null)
                {
                    var currentList = entity.Items;
                    var newList     = itemsToMerge;

                    var removeList = currentList
                                     .Where(currentItem => newList.All(newItem => currentItem.Id != newItem.Id))
                                     .ToList();

                    var addList = newList
                                  .Where(newItem => currentList.All(currentItem => currentItem.Id != newItem.Id))
                                  .ToList();

                    var updateList = currentList
                                     .Join(newList,
                                           currentItem => currentItem.Id,
                                           newItem => newItem.Id,
                                           (currentItem, newItem) => new { CurrentItem = currentItem, NewItem = newItem })
                                     .ToList();

                    foreach (var itemToAdd in addList)
                    {
                        var item = new RequestItem
                        {
                            Quantity        = itemToAdd.Quantity,
                            InitialQuantity = itemToAdd.Quantity,
                            Request         = entity,
                            Title           = itemToAdd.Title,
                            Detail          = itemToAdd.Detail,
                            Type            = itemToAdd.Type
                        };

                        context.RequestItems.Add(item);
                    }

                    foreach (var itemToRemove in removeList)
                    {
                        context.RequestItems.Remove(itemToRemove);
                    }

                    foreach (var item in updateList)
                    {
                        item.CurrentItem.Title           = item.NewItem.Title;
                        item.CurrentItem.Detail          = item.NewItem.Detail;
                        item.CurrentItem.Quantity        = item.NewItem.Quantity;
                        item.CurrentItem.InitialQuantity = item.NewItem.Quantity;
                        item.CurrentItem.Type            = item.NewItem.Type;
                    }
                }

                if (requestOptional != null)
                {
                    entity.Keywords = requestOptional.Keywords;
                    entity.VideoURL = requestOptional.VideoURL;

                    if (requestOptional.Photo != null)
                    {
                        byte[] fileData = null;

                        using (var binaryReader = new BinaryReader(requestOptional.Photo.InputStream))
                        {
                            fileData = binaryReader.ReadBytes(requestOptional.Photo.ContentLength);
                        }

                        entity.Photo = fileData;
                    }
                }

                context.SaveChanges();

                requestMandatory.Id = entity.Id;
            }
        }